【问题标题】:Modify links in a string with PHP用 PHP 修改字符串中的链接
【发布时间】:2015-09-27 07:54:00
【问题描述】:

我正在尝试通过添加重定向来标记字符串中的链接。数据以字符串格式从 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”来获取字符串中所有链接的位置,并将它们存储在一个名为位置的数组中。数组填充了链接开始处的字符,如下所示:

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

【问题讨论】:

  • 使用str_replace全部替换

标签: php string url hyperlink tagging


【解决方案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
);

【讨论】:

  • 这是我最初的想法。
【解决方案2】:

我会使用正则表达式:

$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);

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

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

【讨论】:

    【解决方案3】:

    试试这个

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

    【讨论】:

      【解决方案4】:

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

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

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

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

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

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

      【讨论】:

        【解决方案5】:

        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'));
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-03-22
          • 2011-01-10
          • 1970-01-01
          • 1970-01-01
          • 2018-12-06
          • 1970-01-01
          • 2021-05-14
          • 2013-06-03
          相关资源
          最近更新 更多