是否可以使用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]将成为我的自定义标签
答案 0 :(得分:0)
答案 1 :(得分:0)
试试这段代码:
$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&lt; 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);
答案 2 :(得分:0)
你是如此亲密:
$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
);