Max Frai
Max Frai

Reputation: 64286

Replacing symbol strange situation

foreach($ret as $object)
{
    $res = $object->...;
    $img_src = $res[0]->src;
    echo $img_src . '<br />';
    echo str_replace("&size=2", "", $img_src) . '<br /><br />';
}

$img_src ~ 'http://site.com/img.jpg&size=2'

And I have to receive same link but without &size=2. Why doesn't work my last line in code. It shows the same url.

Upvotes: 0

Views: 53

Answers (3)

Sujit Agarwal
Sujit Agarwal

Reputation: 12508

use preg_replace:

$c=preg_replace("/&size=2/","",$img_src);

Example of usage

<?php
$sr="http://site.com/img.jpg&size=2";
echo preg_replace("/&size=2/","",$sr);
?>

This will output

http://site.com/img.jpg

Upvotes: 0

King Skippus
King Skippus

Reputation: 3826

Are you absolutely certain there are any goofy unprintable characters in your source string? Try debugging with this:

printf("%s\n", join(':', str_split($img_src)));

And make sure you really have &size=2 in your string. If you see two consecutive colons, you've got something like a \0 or some other character mucking up the works in the middle of your string.

Upvotes: 2

tofutim
tofutim

Reputation: 23374

Seems to work on this end:

http://site.com/img.jpg&size=2 
http://site.com/img.jpg

from

<?php
$img_src = 'http://site.com/img.jpg&size=2';
echo $img_src.'<br />';
echo str_replace("&size=2", "", $img_src).'<br/><br/>';
?>

Upvotes: 0

Related Questions