My code is a simple code but i not seing what the problem might be.
I'm doing a "for" to get a number from 1 to 2000, then replacing the number into text, example '1px','2px',...
Then i set this "'1px','2px',..." in a array. For that reason i could verify where in the text has "px" , then i could repalce it with "%". PS: "%" is also an array.
To replace the text, i use str_replace, and it funcional, but not when i use it with my code.
$search = array('150px','151px','152px','153px','154px',...);
$replace = array('100%');
$testes = str_replace($search, $replace, $GetText)
Example of $GetText - <p><img src="data:image/png;base64,iVBOC" data-filename="andy-brown_4.png" style="width: 150px;"><br></p>
this codes work
Now the code that im trying,
for($i = 1; $i<=2000; $i++) {
echo "'". $i . 'px' . "'," ;
$test = "'". $i . 'px' . "'," ;
}
Show all the number from 1 until 2000 with text, example: '274px',
$search = array($test);
$replace = array('100%');
$testes = str_replace($search, $replace, $GetText)
With this process, it doesnt replace the "px" with "%"
You can solve your task with one php function preg_replace
$GetText = '<p><img src="data:image/png;base64,iVBOC" data-filename="andy-brown_4.png" style="width: 150px;"><br></p>';
$result = preg_replace("/\d{1,4}px/","100%",$GetText);
var_dump($result);
What \d{1,4}px
mean is:
\d
- search for digit,
{1,4}
- previous element (e.g. digit) must be repeated from 1 to 4 times,
px
- must be folowed by this text,
So it looks for everything like *px
and replace it with 100%
, where *
is any number from 0
to 9999
;