Michael Luis
Michael Luis

Reputation: 40

Replace Array with Numbers and text

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

Upvotes: 1

Views: 440

Answers (5)

krylov123
krylov123

Reputation: 772

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;

Upvotes: 3

You can do easily with str_ireplace

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

Upvotes: 0

krylov123
krylov123

Reputation: 772

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.

Upvotes: 0

RiggsFolly
RiggsFolly

Reputation: 94662

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';
}

Upvotes: 1

angel.bonev
angel.bonev

Reputation: 2232

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 

Upvotes: 0

Related Questions