简体   繁体   English

php用条件替换href

[英]php replace href by condition

I try to replace href by condition: 我尝试用条件替换href:

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: 我用strpos来判断:

$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. 第二个链接没有按照我的意愿取代。

Oh boy. 好家伙。 You're decisions are based on the results of strpos applied to a string containing all the links. 您的决定是基于应用于包含所有链接的字符串的strpos的结果。 Because $str contains a link of the first type, the corresponding if-condition is always true. 因为$str包含第一种类型的链接,所以相应的if条件始终为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;

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. 这是因为你的所有网址都在同一个字符串中,所以当它通过你的代码时,它匹配“sitea.com”比较,然后它匹配“vip:”比较然后它进入那个并替换所有用“aaa”替换发生“sitea”。 It never gets into the "bbb" replacement line because $str always contains "vip". 它永远不会进入“bbb”替换行,因为$ str总是包含“vip”。

What you will need to do is split your $str into an array of urls and process each one individually. 您需要做的是将$ str拆分为一个url数组并单独处理每个url。

It's because you are trying to parse each url differently, and you are doing them all in the same string. 这是因为您尝试以不同方式解析每个URL,并且您在同一个字符串中执行所有这些操作。 The str_replace is ALWAYS finding :vip, because it's in your entire string. str_replace总是找到:vip,因为它在你的整个字符串中。

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;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM