Php preg_将_回调与扰流板替换为扰流板
我在扰流板内写了类似于Php preg_将_回调与扰流板替换为扰流板,php,regex,preg-replace-callback,Php,Regex,Preg Replace Callback,我在扰流板内写了类似于[spoiler=spoiler Title]的文字[/spoiler] 并使用preg\u replace\u回调(“/\[spoiler=(.*)\](.*)\[\/spoiler\]/Usi”,“BBCode\u spoiler',$text”)要创建真正的扰流板,使用一个或多个扰流板的结果是: <script type="text/javascript"> function show_0() { if(document.getEl
[spoiler=spoiler Title]的文字[/spoiler]
并使用preg\u replace\u回调(“/\[spoiler=(.*)\](.*)\[\/spoiler\]/Usi”,“BBCode\u spoiler',$text”)代码>要创建真正的扰流板,使用一个或多个扰流板的结果是:
<script type="text/javascript">
function show_0() {
if(document.getElementById("0").style.display == "inline-block")
document.getElementById("0").style.display = "none";
else document.getElementById("0").style.display = "inline-block"; }
</script>
<a href="javascript:show_0();"><i>Show Spiler:</i> <b>Spoiler Title</a></b><br>
<div id="0" style="display: none; min-width: 100px; border-bottom: 1px solid #000000; border-right: 1px solid #000000; background-color: #FFFFFF; color: #000000;">
Text inside the Spoiler
</div>
我的回调函数是
<?php
// Spoiler
$counter = 0;
function BBCode_spoiler($hits) {
global $central_lang;
global $counter;
$title = htmlentities(trim($hits[1]));
$text = htmlentities($hits[2]);
$return = "<script type=\"text/javascript\">";
$return .= "function show_".$counter."() {";
$return .= "if(document.getElementById(\"".$counter."\").style.display == \"inline-block\") document.getElementById(\"".$counter."\").style.display = \"none\";";
$return .= "else document.getElementById(\"".$counter."\").style.display = \"inline-block\"; }";
$return .= "</script>";
$return .= "<a href=\"javascript:show_".$counter."();\"><i>".$central_lang['bbcodes']['spoiler']['text'].":</i> <b>".$title."</a></b><br>";
$return .= "<div id=\"".$counter."\" style=\"display: none; min-width: 100px; border-bottom: 1px solid #000000; border-right: 1px solid #000000; background-color: #FFFFFF; color: #000000;\">".$text."</div>";
$counter++;
return $return; }
?>
我尝试的输出结果似乎是这样的
<script type="text/javascript">
function show_0() {
if(document.getElementById("0").style.display == "inline-block")
document.getElementById("0").style.display = "none";
else document.getElementById("0").style.display = "inline-block"; }
</script>
<a href="javascript:show_0();"><i>Show Spoiler:</i> <b>Spoiler Title</a></b><br>
<div id="0" style="display: none; min-width: 100px; border-bottom: 1px solid #000000; border-right: 1px solid #000000; background-color: #FFFFFF; color: #000000;">
<script type="text/javascript">
function show_1() {
if(document.getElementById("1").style.display == "inline-block")
document.getElementById("1").style.display = "none";
else document.getElementById("1").style.display = "inline-block"; }
</script>
<a href="javascript:show_1();"><i>Show Spoiler:</i> <b>Second Spoiler</a></b><br>
<div id="1" style="display: none; min-width: 100px; border-bottom: 1px solid #000000; border-right: 1px solid #000000; background-color: #FFFFFF; color: #000000;">
Another text
</div>
</div>
函数show_0(){
if(document.getElementById(“0”).style.display==“内联块”)
document.getElementById(“0”).style.display=“无”;
else document.getElementById(“0”).style.display=“内联块”;}
函数show_1(){
if(document.getElementById(“1”).style.display==“内联块”)
document.getElementById(“1”).style.display=“无”;
else document.getElementById(“1”).style.display=“内联块”;}
另一个文本
我希望我的问题有答案,谢谢 这通常适用于BBCode:重新运行替换代码,直到它停止更改
preg\u replace\u callback
接受一个“count”引用变量,该变量将被替换次数填充。只要此数字不为零,您就应该重新运行替换(do..while
非常适合此操作)
他们是否被超越并不重要。假设我们有一个BBCode,它用
替换[div]
[div]Blah[div]123[/div]Fish[/div]
更换一次后:
<div>Blah[div]123</div>Fish[/div]
<div>Blah<div>123</div>Fish</div>
Blah[div]123Fish[/div]
再次更换后:
<div>Blah[div]123</div>Fish[/div]
<div>Blah<div>123</div>Fish</div>
blah123鱼
因此,即使它们是按交叉顺序处理的,结果也是正确嵌套的。您可以使用preg_replace_callback的count参数从最内层到最外层处理替换,直到没有更多的标记要替换:
$pattern = '~\[spoiler=([^]]*)]((?>[^[]+|\[(?!/?spoiler\b))*)\[/spoiler]~i';
do {
$result = preg_replace_callback($pattern, 'BBCode_spoiler', $text, -1, $count);
} while ($count>0);
我稍微更改了一下模式,以确保匹配是最里面的标记。谢谢,我终于成功了!我将模式更改为
/\[spoiler=(.*)\](.*)/Usi
,并添加了$text=str\u替换(“[/spoiler]”,“”,$text)代码>在我的preg\u replace\u回调函数之后。。。我没有使用$counter
制作,但它也可以工作!谢谢你问题是,亚历克斯,如果我键入的[/spoiler]
没有开头标签会怎么样?你的网站结构将崩溃*如果$counter
大于0;,它可以更改[/spoiler]
)<代码>[spoiler=测试]/spoiler][/spoiler][/spoiler]
,效果?会的,但只有一次