我在使用正则表达式麻烦
,我是一个绝对的正则表达式的小白。我无法看到尝试将HTML转换回“BBCode”时发生了什么问题。
有人可以看看'unquote'函数,并告诉我我正在犯的明显错误吗? (我知道这很明显,因为我总是发现不明显的错误)
注意:我没有使用递归正则表达式,因为我无法得到我的头,并且已经开始这种方式来整理行情他们嵌套。
<?php
function quote($str){
$str = preg_replace('@\[(?i)quote=(.*?)\](.*?)@si', '<div class="quote"><div class="quote-title">\\1 wrote:</div><div class="quote-inner">\\2', $str);
$str = preg_replace('@\[/(?i)quote\]@si', '</div></div>', $str);
return $str;
}
function unquote($str){
$str = preg_replace('@\<(?i)div class="quote"\>\<(?i)div class="quote_title"\>(.*?)wrote:\</(?i)div\><(?i)div class="quote-inner"\>(.*?)@si', '[quote=\\1]\\2', $str);
$str = preg_replace('@\</(?i)div\></(?i)div\>@si', '[/quote]', $str);
}
?>
这仅仅是一些代码,以帮助测试:
<html>
<head>
<style>
body {
font-family: sans-serif;
}
.quote {
background: rgba(51,153,204,0.4) url(../img/diag_1px.png);
border: 1px solid rgba(116,116,116,0.36);
padding: 5px;
}
.quote-title, .quote_title {
font-size: 18px;
margin: 5px;
}
.quote-inner {
margin: 10px;
}
</style>
</head>
<body>
<?php
$quote_text = '[quote=VCMG][quote=2xAA]DO RECURSIVE QUOTES WORK?[/quote]I have no idea.[/quote]';
$quoted = quote($quote_text);
echo $quoted.'<br><br>'.unquote($quoted); ?>
</body>
提前,山姆感谢。
> _>该死的回报......我知道这是明显的东西。 并感谢您的简化,好多了! – 2xAA