【发布时间】:2015-10-16 22:54:21
【问题描述】:
我有以下代码,它应该使纯文本链接可点击。但是,如果有多个链接,它只会替换最后一个。
代码:
$nc = preg_match_all('#<pre[\s\S]*</pre>#U', $postbits, $matches_code);
foreach($matches_code[0] AS $match_code)
{
$match = null;
$matches = null;
$url_regex = '#https?://(\w*:\w*@)?[-\w.]+(:\d+)?(/([\w/_.]*(\?\S+)?)?)?[^<\.,:;"\'\s]+#';
$n = preg_match_all($url_regex, $match_code, $matches);
foreach($matches[0] AS $match)
{
$html_url = '<a href="' . $match . '" target="_blank">' . $match . '</a>';
$match_string = str_replace($match, $html_url, $match_code);
}
$postbits = str_replace($match_code, $match_string, $postbits);
}
结果:
http://www.google.com
http://www.yahoo.com
http://www.microsoft.com/ <-- only this one is clickable
预期结果:
我的错误在哪里?
【问题讨论】:
-
[\s\S]的意义何在?这匹配所有内容,就像.。如果问题是它不匹配换行符,请使用s修饰符。
标签: php foreach preg-match-all