使用PHP修改字符串中的链接

时间:2015-07-09 05:40:32

标签: php string url hyperlink tagging

我试图通过添加重定向来标记字符串中的链接。数据以字符串格式出自MySQL数据库,看起来像这样:

$string = "<p><a href='http://twitter.com'>Follow on Twitter</a>&nbsp;&nbsp;and please friend on&nbsp;<a href='http://facebook.com'>Friend on Facebook</a></p>";

我正在使用strpos函数和针“http”来获取字符串中所有链接的位置,并将它们存储在一个名为position的数组中。数组填充了链接开始的字符,如下所示:

Array
(
    [0] => 12
    [1] => 100
)

然后我遍历位置数组并使用substr_replace在http之前添加重定向链接。但是,这仅适用于一个链接,如果我在字符串中有多个链接,则会被覆盖。任何人都有这个聪明的解决方案吗?

这是我的代码:

function stringInsert($str,$pos,$insertstr)
{
    if (!is_array($pos))
        $pos=array($pos);

    $offset=-1;
        foreach($pos as $p)
        {
            $offset++;
            $str = substr($str, 0, $p+$offset) . $insertstr . substr($str, $p+$offset);

        }
    return $str;
}

$string = "<p><a href='http://twitter.com'>Follow on Twitter</a>&nbsp;&nbsp;and please friend on&nbsp;<a href='http://facebook.com'>Friend on Facebook</a></p>";
$needle = "http";
$lastPos = 0;
$positions = array();

while (($lastPos = strpos($string, $needle, $lastPos))!== false) {
    $positions[] = $lastPos;
    $lastPos = $lastPos + strlen($needle);
}

$str_to_insert = "http://redirect.com?link=";

foreach ($positions as $value) {
    $finalstring = substr_replace($string, $str_to_insert, $value, 0);
}

最终结果如下:

$string = "<p><a href='http://redirect.com?link=http://twitter.com'>Follow on Twitter</a>&nbsp;&nbsp;and please friend on&nbsp;<a href='http://redirect.com?link=http://facebook.com'>Friend on Facebook</a></p>";

5 个答案:

答案 0 :(得分:1)

我认为更舒适的解决方案可能是使用str_replace(http://php.net/manual/en/function.str-replace.php

做这样的事情:

$string = str_replace(
    ['http://','https://'],
    ['http://redirect.com?link=http://', 'http://redirect.com?link=https://'],
    $sourceString
);

答案 1 :(得分:1)

我选择regexp:

$str = "<p><a href='http://twitter.com'>Follow on Twitter</a>  and please friend on <a href='http://facebook.com'>Friend on Facebook</a></p>";

$str = preg_replace('/([\'\"])(http[^\'\"]+)([\'\"])/', '$1http://redirect.com?link=$2$3', $str);

echo htmlspecialchars($str);

我得到的输出:<p><a href='http://redirect.com?link=http://twitter.com'>Follow on Twitter</a> and please friend on <a href='http://redirect.com?link=http://facebook.com'>Friend on Facebook</a></p>

请记住,最后一行仅用于显示目的。使用Escaped html以便您查看结果。对于实际工作的链接,您不需要htmlspecialchars来电。

答案 2 :(得分:0)

试试这个

str_replace("href='", "href='http://redirect.com?link=", $string);

答案 3 :(得分:0)

相反,让我们使用DOMDocument解析器,它允许我们针对HTML字符串利用规范化方法。

$doc = new DOMDocument();
$doc->loadHTML($string);

现在我们可以遍历所有锚元素并进行必要的修改:

foreach( $doc->getElementsByTagName("a") as $anchor) {
    $newLink = "http://example.com";
    $anchor->setAttribute('href', $newLink );
}

然后,您可以在完成后执行echo $doc->saveHTML();

您也可以在实际的foreach循环中执行条件比较。

答案 4 :(得分:0)

Jquery这样做的方法:

<强> HTML

<p id="addRedirect"><a href='http://twitter.com'>Follow on Twitter</a>&nbsp;&nbsp;and please friend on&nbsp;<a href='http://facebook.com'>Friend on Facebook</a></p>

Jquery代码

$('#addRedirect > a').each(function(){
    $(this).attr('href','http://redirect.com?link=' + $(this).attr('href'));
});
相关问题