我的网站最近崩溃了,因为eregi_replace被淘汰了。现在,我需要将它们转换为preg_replace或其他格式。这对我的大多数函数都有效,但我在模拟bbtag的函数中遇到了问题。
有人能帮帮忙吗?
$txt = eregi_replace("\\[url\\]([^\\[]*)\\[/url\\]", "<a href=\"\\1\" target=\"_blank\">\\1</a>", $txt);
$txt = eregi_replace("\\[img\\]([^\\[]*)\\[/img\\]", "<a href=\"\\1\"><img src=\"\\1\" width=\"150px\" height=\"150px\" style=\"float: left; margin-right: 10px;\" /></a>", $txt);
$txt = eregi_replace("\\[cimg\\]([^\\[]*)\\[/cimg\\]", "<a href=\"\\1\"><img src=\"\\1\" width=\"100px\" height=\"100px\" /></a>", $txt);
$txt = eregi_replace("\\[code\\]([^\\[]*)\\[/code\\]", "<br><br><strong>Code:</strong><table width=\"80%\" border=\"0\" cellpadding=\"3\" cellspacing=\"0\" bgcolor=\"#FFFFFF\" style=\"border:1px solid gray;\"><tr><td bgcolor=\"#FFFFFF\"><font color=\"#009900\" size=\"-2\">\\1</font></td></tr></table><br>", $txt);发布于 2013-06-29 21:48:27
例如:
$txt = preg_replace('~\[url]([^[]*+)\[/url]~i', '<a href="$1" target="_blank">$1</a>', $txt);*+表示零次或更多次(possessive)
注意:您不需要转义右方括号。您不需要转义字符类中的左方括号。在这里,使用简单的引号将字符串括起来是最好的选择,因为您不必在其中转义双引号。
使用\[^]]而不是点的好处是,您可以避免使用惰性量词.*?,并获得更高性能的模式。第二个有趣的地方是避免dotall问题,因为这个字符类匹配换行符。
https://stackoverflow.com/questions/17380834
复制相似问题