4

我似乎在用发布的网站链接替换文本链接时遇到问题,它没有链接。

编码:

$status_text = preg_replace('#(\A|[^=\]\'"a-zA-Z0-9])(http[s]?://(.+?)/[^()<>\s]+)#i', '\\1<a href="\\2">\\3</a>', $status_text);
echo $status_text;

$status_text从名为 的 MySQL 字段中提取contents,并包含其他文本,但我只想链接链接。此外,我还希望它不显示完整的 URL,只显示主域。

更新:我们在同一页面上有另外两个 preg_replaces,在它们前面寻找带有 + 和 # 的链接到站点区域的东西,它们目前可以工作并且不需要与上述内容冲突:

$status_text = preg_replace("/#([a-z_0-9]+)/i", "<a href=\"http://url.com/hashlink/$1\">$0</a>", $status_text);


$status_text = preg_replace("/\+([a-z_0-9]+)/i", "<a href=\"http://url.com/pluslink/$1\">$0</a>", $status_text);
4

2 回答 2

9

在这里,试试这个:

$status_text = preg_replace('|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '$1 <a href="$2">$3</a>', $status_text);
echo $status_text;

或者让它更容易阅读:

$m = '|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
$r = '$1 <a href="$2">$3</a>';

$status_text = preg_replace($m,$r,$status_text);
echo $status_text;

编辑——由于OP中的新信息而更新——

您的主题标签正则表达式不考虑 URL 中的哈希值,所以让我们解决这个问题......此外,您应该在匹配主题标签或加号标签之前匹配 URL,否则您会弄乱为哈希创建的链接-标签和加号标签

$status_text = preg_replace('|(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '<a href="$1">$2</a>', $status_text);
$status_text = preg_replace('|\B#([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
$status_text = preg_replace('|\B\+([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);

或者让它更容易阅读......

$match_href = '|(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
$match_hash = '|\B#([\d\w_]+)|i';
$match_plus = '|\B\+([\d\w_]+)|i';
$replace_url = '<a href="$1">$2</a>';
$replace_tag = '<a href="http://url.com/pluslink/$1">$0</a>';

$status_text = preg_replace($match_href, $replace_url, $status_text);
$status_text = preg_replace($match_hash, $replace_tag, $status_text);
$status_text = preg_replace($match_plus, $replace_tag, $status_text);

再次编辑——添加一个可能有用的 URL——

您可以在这里测试正则表达式:http: //gskinner.com/RegExr/

另一个编辑

根据评论/问题,如果您想考虑缺少 http 协议的 url,请使用以下命令:

$status_text = preg_replace('|((https?://)?([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '<a href="$1">$3</a>', $status_text);
$status_text = preg_replace('|\B#([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);
$status_text = preg_replace('|\B\+([\d\w_]+)|i', '<a href="http://url.com/pluslink/$1">$0</a>', $status_text);

或者让它更容易阅读......

$match_href = '|((https?://)?([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i';
$match_hash = '|\B#([\d\w_]+)|i';
$match_plus = '|\B\+([\d\w_]+)|i';
$replace_url = '<a href="$1">$3</a>';
$replace_tag = '<a href="http://url.com/pluslink/$1">$0</a>';

$status_text = preg_replace($match_href, $replace_url, $status_text);
$status_text = preg_replace($match_hash, $replace_tag, $status_text);
$status_text = preg_replace($match_plus, $replace_tag, $status_text);

示例用法

输入:(来自 DB 的文本 -> $status_text)

<!-- language-all: lang-none -->
Hi, this is an example. This is a url http://stackoverflow.com/ 
and this is a hash reference that we want to link to an internal 
post #AwesomePost123 and this one is a plus reference we want to 
link to an internal post +AwesomePost123 and finally an example 
of a url without the http protocol www.stackoverflow.com

输出:(运行正则表达式后)

<!-- language-all: lang-none -->
Hi, this is an example. This is a url <a href="http://stackoverflow.com/">stackoverflow.com</a> 
and this is a hash reference that we want to link to an internal 
post <a href="http://url.com/pluslink/AwesomePost123">#AwesomePost123</a> and this one is a plus reference we want to 
link to an internal post <a href="http://url.com/pluslink/AwesomePost123">+AwesomePost123</a> and finally an example 
of a url without the http protocol <a href="www.stackoverflow.com">www.stackoverflow.com</a>
于 2012-10-28T03:23:46.420 回答
0

你可以试试这个:

function makeClickableLinks($s) {
  return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $s);
}

要测试它,试试这个:

echo makeClickableLinks('Hello world, you go to www.google.com to check! also http://www.google.com/ works too!');

在这里演示:http: //codepad.viper-7.com/hkOdCM

于 2012-10-28T03:41:44.223 回答