php reemplaza href por condición

I try to replace href by condition:

http://www.sitea.com/vip:  => http://localhost/aaa?search=
http://www.sitea.com/      => http://localhost/bbb/ (without 'vip:')
http://www.siteb.com/      => http://localhost/ccc/

yo suelo strpos for judge:

$str='<a href="http://www.sitea.com/vip:vp_14098">link A</a><a href="http://www.sitea.com/contact">link B</a><a href="http://www.siteb.com/player">link C</a>';
if(strpos($str,'www.sitea.com')!== false){
    if(strpos($str,'vip:')!== false){
        $str = str_replace('http://www.sitea.com/', 'http://localhost/aaa?search=',$str);
    }else{
        $str = str_replace('http://www.sitea.com/', 'http://localhost/bbb/',$str);
    }
}
if(strpos($str,'www.siteb.com')!== false){
    $str = str_replace('http://www.siteb.com/', 'http://localhost/ccc/',$str);
}
echo $str;

But this output:

<a href="http://localhost/aaa?search=vip:vp_14098">link A</a>
<a href="http://localhost/aaa?search=contact">link B</a>
<a href="http://localhost/ccc/player">link C</a>

The second link not replace as I wish.

preguntado el 09 de marzo de 12 a las 22:03

3 Respuestas

Oh boy. You're decisions are based on the results of strpos applied to a string containing todos the links. Because $str contains a link of the first type, the corresponding if-condition is always true. You can use a regular expression to address that problem, or if this isn't just a simplified example but your real world code, try this:

$str='<a href="http://www.sitea.com/vip:vp_14098">link A</a><a href="http://www.sitea.com/contact">link B</a><a href="http://www.siteb.com/player">link C</a>';
$str = str_replace('http://www.sitea.com/vip:', 'http://localhost/aaa?search=vip:',$str);
$str = str_replace('http://www.sitea.com/', 'http://localhost/bbb/',$str);
$str = str_replace('http://www.siteb.com/', 'http://localhost/ccc/',$str);
echo $str;

respondido 09 mar '12, 22:03

if there are more links in $str how to make a difference between http://www.sitea.com/vip: y http://www.sitea.com/ , str_replace will replace from top to the end. thanks. - cj333

Si reemplazas http://www.sitea.com/vip: first, those are gone from the string, right? So if you replace all occurrences of http://www.sitea.com/ afterwards, those with vip: at the end won't be touched because they are already replaced by something else. I'm not quite sure if this answers your question, but otherwise you will need to clarify what the exact problem is. - Niko

This is because all of your urls are in the same string, so as it goes through your code, it matches the "sitea.com" comparison, and then it matches the "vip:" comparison and then it goes into that and replaces all occurrences of "sitea" with your "aaa" replacement. It never gets into the "bbb" replacement line because $str always contains "vip".

What you will need to do is split your $str into an array of urls and process each one individually.

respondido 09 mar '12, 22:03

It's because you are trying to parse each url differently, and you are doing them all in the same string. The str_replace is ALWAYS finding :vip, because it's in your entire string.

There will be a post here that will probably give you some regular expressions to use, but that is not my area of expertise. Instead, I will offer you these changes to your code:

<?php
    $str='<a href="http://www.sitea.com/vip:vp_14098">link A</a><a href="http://www.sitea.com/contact">link B</a><a href="http://www.siteb.com/player">link C</a>';
    $str = str_replace('http://www.sitea.com/vip:', 'http://localhost/aaa?search=',$str);
    $str = str_replace('http://www.sitea.com/', 'http://localhost/bbb/',$str);
    $str = str_replace('http://www.siteb.com/', 'http://localhost/ccc/',$str);
    echo $str;
?>

respondido 09 mar '12, 22:03

if there are more links in $str how to make a difference between http://www.sitea.com/vip: y http://www.sitea.com/ , debo usar preg_match to make a judge instead of strpos?. thanks. - cj333

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.