[英]preg_replace: BB code to HTML URL
I have a little script here which replaces BB code with HTML code. 我这里有一个小脚本,用HTML代码代替了BB代码。 Everything works fine but the URLs. 一切正常,但URL。
$bbextended = array(
"/\[URL=(.*?)\](.*?)\[\/URL\]/i" => "<a href=\"$1\" title=\"$1\">$2</a>"
);
foreach($bbextended as $match=>$replacement){
$bbtext = preg_replace($match, $replacement, $bbtext);
}
Input 输入
[URL="http://somewebsite.come/something"]Some Website Title[/URL]
Output 产量
<a href=""http://somewebsite.come/something"" title=""http://somewebsite.come/something"">Some Website Title</a>
There are double-quotes, which obviously isn't that good. 有双引号,这显然不是那么好。
I tried 我试过了
$bbextended = array(
"/\[URL=\"(.*?)\"\](.*?)\[\/URL\]/i" => "<a href=\"$1\" title=\"$1\">$2</a>"
);
in the code but it didn't work. 在代码中,但是没有用。 I also tried to leave out the escape sign and quotes around the $1
in the HTML code but it didn't work neither. 我还尝试过在HTML代码中省略转义符和$1
周围的引号,但两者均无效。
Any ideas? 有任何想法吗?
您应该为此使用真正的解析器,例如jBB http://jbbcode.com/
When I 当我
Set the Find string = '/\\[URL="(.*?)"\\](.*?)\\[\\/URL\\]/i'
设置查找字符串= '/\\[URL="(.*?)"\\](.*?)\\[\\/URL\\]/i'
= '/\\[URL="(.*?)"\\](.*?)\\[\\/URL\\]/i'
\\ '/\\[URL="(.*?)"\\](.*?)\\[\\/URL\\]/i'
\\ '/\\[URL="(.*?)"\\](.*?)\\[\\/URL\\]/i'
\\ '/\\[URL="(.*?)"\\](.*?)\\[\\/URL\\]/i'
and 和
Set the replace string = '<a href="$1" title="$1">$2</a>'
设置替换字符串= '<a href="$1" title="$1">$2</a>'
I get this using simple preg_replace 我使用简单的preg_replace来获得
<a href="http://somewebsite.come/something" title="http://somewebsite.come/something">Some Website Title</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.