爆炸和str_replace

I have the following code

while (strpos($r, "[url]") !== FALSE){
    $link = explode("[url]", $r);
    $final_link = explode("[/url]", $link[0]);

    $thelink = $final_link[0];
}
$r = str_replace("[url]","[url=$thelink]",$r);

If $r = [url]www.google.com[/url], I need to turn this into [url=www.google.com]www.google.com[/url]

This code doesn't seem to be doing it. Have I missed something? Give me a 500 error!

Thanks

preg_replace('~\[url\](.*?)\[/url\]~', '[url=$1]$1[/url]', $string);

Here you go:

$r = '[url]www.google.com[/url]';
if (strpos($r, "[url]") !== FALSE){
    $link = explode("[url]", $r);
    $final_link = explode("[/url]", $link[1]);

    $thelink = $final_link[0];
    $r = str_replace("[url]","[url=$thelink]",$r);
}

var_dump($r);

Changes are: changed the while to an if as there doesn't seem to be a reason for it being a while. Also changed $link[0] to $link[1].

Although this doesn't directly answer your (not very good!) question I would recommend using a regular expression instead:

$outputText = preg_replace('/\[url\](.*?)\[\/url\]/i', '[url=$1]$1[/url]', $inputText);
$r = preg_replace('#\[url\](.*?)\[/url\]#', '[url=$1]$1[/url]', $r);

Try that regular expression instead.

$r = "[url]www.google.com[/url]";
$sub="=".substr($r,5,-6);
echo $url=substr_replace($r,$sub,4,0);