1

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', enter image description here

$search = array($test);
$replace = array('100%');
$testes = str_replace($search, $replace, $GetText)

With this process, it doesnt replace the "px" with "%"

enter image description here

Michael Luis
  • 40
  • 10

5 Answers5

3

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;

krylov123
  • 739
  • 8
  • 15
1

If you want an array out of this loop, then build an array and not a string.

Incidentally doing $test = "'". $i . 'px' . "'," ; will overwrite the $test variable each time round the loop, which means you would have ended up with a single value in that string i.e. $test = '2000px'

for($i = 1; $i<=2000; $i++) {
    echo "'". $i . 'px' . "'," ;
    //$test = "'". $i . 'px' . "'," ;
    $test[] = "{$i}px";
}

or if you prefer

for($i = 1; $i<=2000; $i++) {
    $test[] = $i . 'px';
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

As you can see here str_replace accepts array "This function returns a string or an array with all occurrences of search in subject replaced with the given replace value." , but i think in you case you can use preg_replace

$search = []; // define search array
$GetText = '<p><img src="data:image/png;base64,iVBOC" data-filename="andy-brown_4.png" style="width: 150px;"><br></p>';//dummy text
for($i = 1; $i<=2000; $i++) {
   $search[] =  " ". $i . 'px' ; // this will push new element into the array
}  

$testes = str_replace($search, ' 100%', $GetText); // this will change every element from $search array to ' 100%'
var_dump($testes); // returns <p><img src="data:image/png;base64,iVBOC" data-filename="andy-brown_4.png" style="width: 100%;"><br></p>

var_dump(preg_replace('/(\d+)px/i','100%',$GetText));// this will give you the same result 
angel.bonev
  • 2,154
  • 3
  • 20
  • 30
  • @RiggsFolly this is the mistery with str_replace try this str_replace(["50px", "150px"], ' 100%', $GetText); it will return "width: 1 100%;" ... etc – angel.bonev Aug 20 '19 at 10:02
  • @RiggsFolly i really don't like that str_replace with 2k elements – angel.bonev Aug 20 '19 at 10:07
  • Me neither, but without a really good idea about what the OP is really trying to do it is difficult/dangerous to make other suggestions :) – RiggsFolly Aug 20 '19 at 10:10
0

Your want an array, but handle a string here:

$test = "'".$i.'px'."',";

This code always rewrite $test string, so $test always equal to '2000px',.

But you need to fill an array like this:

$test[] =  $i.'px';

Be aware that you don't need a quotes here, because str_replace will be looking for quotes in text (I believe in most cases it will not find them).

The next thing that you need to go from 2000 to 1, from greater to lesser, because str_replace will replace first finded occurence.

In case you have 210px, str_replace will find 10px and replace it with your 100% and you got 2100%.

So, working code could looks like this:

$GetText = '<p><img src="data:image/png;base64,iVBOC" data-filename="andy-brown_4.png" style="width: 150px;"><br></p>';

$search = array();

for($i = 2000; $i>=1; $i--) {
    $search[] = $i . 'px';
}

$replace = '100%';

$result = str_replace($search, $replace, $GetText);

var_dump($result);

Hope that was what you looking for.

krylov123
  • 739
  • 8
  • 15
0

You can do easily with str_ireplace

search = array($test);
$replace = array('100%');
$testes = str_ireplace($search, $replace, $GetText);

dılo sürücü
  • 3,821
  • 1
  • 26
  • 28