Php replace href by condition

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/

I use strpos
for judge:

$str='link Alink Blink C';
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:

link A
link B
link C

The second link not replace as I wish.

Oh boy. You’re decisions are based on the results of strpos applied to a string containing all
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='link Alink Blink C';
$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;
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Php replace href by condition

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录