1

我需要帮助将链接替换为“ http://www.link.com ”格式

例如:从

    $output_string = 'fix me <a href="www.link.com">link</a> or fix me <a href="link.com">link</a> and fix me too <a href="http://link.com">link</a> and replace me with <a href="http://www.link.com>link</a>';

    $output_string = 'fix me <a href="http://www.link.com">link</a> or fix me <a href="http://www.link.com">link</a> and fix me too <a href="http://www.link.com">link</a> and replace me with <a href="http://www.link.com>link</a>';
4

3 回答 3

1

您可能不想重写http://anylinks.comhttp://www.anylinks.com因为某些域不能使用www前缀。

于 2013-06-04T12:53:37.507 回答
1

您可以使用str_replace函数来实现这一点

$str = 'fix me <a href="www.link.com">link</a> or fix me <a href="link.com">link</a> and fix me too <a href="http://link.com">link</a> and replace me with <a href="http://www.link.com>link</a>';
echo str_replace('www.www', 'www', str_replace('http://www.http://', 'http://www.', str_replace('a href="', 'a href="http://www.', $str)));

这将输出

'fix me <a href="http://www.link.com">link</a> or fix me <a href="http://www.link.com">link</a> and fix me too <a href="http://www.link.com">link</a> and replace me with <a href="http://www.link.com>link</a>'

现场演示

于 2013-06-04T12:56:09.593 回答
0
$output_string = preg_replace('/(href=\")(http:\/\/)?/i', '$1http://', $output_string);

此正则表达式将在字符串中查找 href 标记,并添加 http://(如果尚未存在)。最好搜索“www”,因为 URL 可能不以 www 开头。它不区分大小写。

于 2013-06-04T12:56:07.787 回答