php q2a软件www.question2swer.org使用以下preg_replace解析文本中的链接:
function qa_html_convert_urls($html, $newwindow=false) {
return substr(preg_replace('/([^A-Za-z0-9])((http|https|ftp):'/'/([^'s&<>"'''.])+'.([^'s&<>"'']|&)+)/i', ''1<A HREF="'2" '.($newwindow ? ' target="_blank"' : '').'>'2</A>', ' '.$html.' '), 1, -1);
}
唯一的问题是诸如"看这里http://www.mydomain.com/."将像https://www.mydomain.com/.
或"查看此处"链接(http://www.mydomain.com/)"将链接http://www.mydomain.com/)
,最后一个字符(句点、逗号或括号)将成为链接的一部分。并断开正确的链接。
你能为这个问题提供一个解决方案吗?非常感谢:)
工作解决方案是:
function qa_html_convert_urls($html, $newwindow=false) {
return substr(preg_replace('/([^A-Za-z0-9])((http|https|ftp):'/'/([^'s&<>'(')'[']"'''.])+'.([^'s&<>'(')'[']"'']|&)+)/i', ''1<a href="'2" '.($newwindow ? ' target="_blank"' : '').'>'2</a>', ' '.$html.' '), 1, -1);`
}
感谢吉德格林。