是否可以像我尝试做的那样使用 php 创建自定义标签
$str="[code] Code will goes here [/code]"
echo preg_replace("<div style='background-color:yellow;padding:5px'>$1</div>","/\[code\](.+)\[\/code\]/i",$str);
所以 [code] 将是我的自定义标签
试试这个代码:
$str = "[code] Code goes here, and it can safely contain <html> tags [/code]";
echo preg_replace_callback(
'#\[code\](.+?)\[/code\]#i',
function($matches) {
return "<div style='background-color:yellow;padding:5px'>".htmlspecialchars(trim($matches[1]))."</div>";
},
$str
);
...或对于 PHP < 5.3:
function bbcode_code_tag($matches) {
return "<div style='background-color:yellow;padding:5px'>".htmlspecialchars(trim($matches[1]))."</div>";
}
$str = "[code] Code goes here, and it can safely contain <html> tags [/code]";
echo preg_replace_callback('#\[code\](.+?)\[/code\]#i', 'bbcode_code_tag', $str);
你是如此接近:
$str = "[code] Code will goes here [/code]";
//Pattern, Replacement, Original String
echo preg_replace(
"/\[code\](.*?)\[\/code\]/",
'<div style="background-color:yellow;padding:5px">$1</div>',
$str
);