我正在尝试模拟一个 bbcode 标签,如下面的代码:
[code]this is code to render[/code]
[code attributeA=arg]this is code to render[/code]
[code attribute C=arg anotherAtributte=anotherArg]this is code to render[/code]
如您所见,代码标签可以根据需要获取任意数量的属性,也可以在同一个“发布”中存在过多的代码标签。我只处理过最简单的标签,如 img、b、a、i。例如:
$result = preg_replace('#\[link\=(.+)\](.+)\[\/link\]#iUs', '<a href="$1">$2</a>', $publishment);
这很好用,因为它返回最终标记。但是,在代码标签中,我需要在数组中包含“属性”和“值”,以便根据这些属性自己构建标记,以便模拟这样的事情:
$code_tag = someFunction("[code ??=?? ...] content [/code]", $array );
//build the markup myself
$attribute1 = array_contains("attribute1", $array)? $array["attribute1"] : "";
echo '<pre {$attribute1}>' . $array['content'] . </pre>
所以,我不指望你完全为我做这件事,我需要你帮助我走向正确的方向,因为我从来没有使用过正则表达式。
先感谢您