问题描述
|
我正在尝试运行
preg_replace()
函数来替换页面字符串/内容中两个自定义标签(即[xcode]
)之间的内容。
我要对这些自定义标签之间的内容进行的操作是通过highlight_string()
函数运行它,并从输出中删除那些自定义标签。
知道怎么做吗?
解决方法
因此,您需要某种BBCode解析器。下面的示例将“ 1”标签替换为您喜欢的任何标记。
<?php
function highlight($text) {
$text = preg_replace(\'#\\[xcode\\](.+?)\\[\\/xcode\\]#msi\',\'<em>\\1</em>\',$text);
return $text;
}
$text = \'[xcode]Lorem ipsum[/xcode] dolor sit [xcode]amet[/xcode].\';
echo highlight($text);
?>
如果要将匹配的文本传递给函数,请使用preg_replace_callback()
:
<?php
function parse($text) {
$text = preg_replace_callback(\'#\\[xcode\\](.+?)\\[\\/xcode\\]#msi\',function($matches) {
return highlight_string($matches[1],1);
},$text);
return $text;
}
$text = \'[xcode]Lorem ipsum[/xcode] dolor sit [xcode]amet[/xcode].\';
echo bbcode($text);
?>
我将包含我很久以前制作的BBCode解析器的源代码。随意使用它。
<?php
function bbcode_lists($text) {
$pattern = \"#\\[list(\\=(1|a))?\\](.*?)\\[\\/list\\]#msi\";
while (preg_match($pattern,$text,$matches)) {
$points = explode(\"[*]\",$matches[3]);
array_shift($points);
for ($i = 0; $i < count($points); $i++) {
$nls = split(\"[\\n]\",$points[$i]);
$brs = count($nls) - 2;
$points[$i] = preg_replace(\"[\\r\\n]\",\"<br />\",$points[$i],$brs);
}
$replace = ($matches[2] != \'1\') ? ($matches[2] != \'a\') ? \'<ul>\' : \'<ol style=\"list-style:lower-alpha\">\' : \'<ol style=\"list-style:decimal\">\';
$replace .= \"<li>\";
$replace .= implode(\"</li><li>\",$points);
$replace .= \"</li>\";
$replace .= ($matches[2] == \'1\' || $matches[2] == \'a\' ) ? \'</ol>\' : \'</ul>\';
$text = preg_replace($pattern,$replace,1);
$text = preg_replace(\"[\\r\\n]\",\"\",$text);
}
return $text;
}
function bbcode_parse($text) {
$text = preg_replace(\"[\\r\\n]\",$text);
$smilies = Array(
\':)\' => \'smile.gif\',\':d\' => \'tongue2.gif\',\':P\' => \'tongue.gif\',\':lol:\' => \'lol.gif\',\':D\' => \'biggrin.gif\',\';)\' => \'wink.gif\',\':zzz:\' => \'zzz.gif\',\':confused:\' => \'confused.gif\'
);
foreach ($smilies as $key => $value) {
$text = str_replace($key,\'<img src=\"/images/smilies/\' . $value . \'\" alt=\"\' . $key . \'\" />\',$text);
}
if (!(!strpos($text,\"[\") && !strpos($text,\"]\"))) {
$bbcodes = Array(
\'#\\[b\\](.*?)\\[/b\\]#si\' => \'<strong>$1</strong>\',\'#\\[i\\](.*?)\\[/i\\]#si\' => \'<em>$1</em>\',\'#\\[u\\](.*?)\\[/u\\]#si\' => \'<span class=\"u\">$1</span>\',\'#\\[s\\](.*?)\\[/s\\]#si\' => \'<span class=\"s\">$1</span>\',\'#\\[size=(.*?)\\](.*?)\\[/size\\]#si\' => \'<span style=\"font-size:$1\">$2</span>\',\'#\\[color=(.*?)\\](.*?)\\[/color\\]#si\' => \'<span style=\"color:$1\">$2</span>\',\'#\\[url=(.*?)\\](.*?)\\[/url\\]#si\' => \'<a href=\"$1\" target=\"_blank\">$2</a>\',\'#\\[url\\](.*?)\\[/url\\]#si\' => \'<a href=\"$1\" target=\"_blank\">$1</a>\',\'#\\[img\\](.*?)\\[/img\\]#si\' => \'<img src=\"$1\" alt=\"\" />\',\'#\\[code\\](.*?)\\[/code\\]#si\' => \'<div class=\"code\">$1</div>\'
);
$text = preg_replace(array_keys($bbcodes),$bbcodes,$text);
$text = bbcode_lists($text);
$quote_code = Array(\"\'\\[quote=(.*?)\\](.*?)\'i\",\"\'\\[quote](.*?)\'i\",\"\'\\[/quote\\]\'i\");
$quote_html = Array(\'<blockquote><p class=\"quotetitle\">Quote \\1:</p>\\2\',\'<blockquote>\\2\',\'</blockquote>\');
$text = preg_replace($quote_code,$quote_html,$text);
}
return $text;
}
?>
, 基本上,
preg_replace_callback(\'~\\[tag\\](.+?)\\[/tag\\]~\',function($matches) { whatever },$text);
这虽然不处理嵌套标签
完整的例子
$text = \"hello [xcode] <? echo bar ?> [/xcode] world\";
echo preg_replace_callback(
\'~\\[xcode\\](.+?)\\[/xcode\\]~\',function($matches) {
return highlight_string($matches[1],1);
},$text
);
, <?php
$string = \'The quick brown fox jumped over the lazy dog.\';
$patterns = array();
$patterns[0] = \'/quick/\';
$patterns[1] = \'/brown/\';
$patterns[2] = \'/fox/\';
$replacements = array();
$replacements[2] = \'bear\';
$replacements[1] = \'black\';
$replacements[0] = \'slow\';
echo preg_replace($patterns,$replacements,$string);
?>
上面的示例将输出:
熊黑慢的跳了过来。
http://php.net/manual/zh/function.preg-replace.php
要么
str_replace应该可以帮助您
http://php.net/manual/zh/function.str-replace.php
, 多亏了user187291的建议和preg_replace_callback规范,我最终获得了以下结果,可以胜任工作! :
function parseTagsRecursive($input)
{
$regex = \'~\\[xcode\\](.+?)\\[/xcode\\]~\';
if (is_array($input)) {
$input = highlight_string($input[1],true);
}
return preg_replace_callback($regex,\'parseTagsRecursive\',$input);
}
$text = \"hello [xcode] <? echo bar ?> [/xcode] world and [xcode] <?php phpinfo(); ?> [/xcode]\";
echo parseTagsRecursive($text);
通过此函数解析$ text变量的输出为:
hello <? echo bar ?> world and <?php phpinfo(); ?>
谢谢大家的投入!