如何使用 preg_replace php 将 <div style= "xyz" > 转换为 [div style= "xyz" ]



我有一个html字符串

$html = '<div style="background-color:#000;border:1px solid #000">
<b>Some Text</b></div><span>I have amount > 1000 USD</span>';

我想把它转换成这个

$html = '[div style="background-color:#000;border:1px solid #000"]
[b]Some Text[/b][/div][span]I have amount > 1000 USD[/span]';

我在谷歌上搜索了很多,想得到一些将html转换为bbcode的php脚本,但找不到。我不知道正则表达式。如果你给我一些想法,用示例代码,它会给我启动。

如果可以用其他php函数来完成,请建议我这样做。

使用此

$html = '<div style="background-color:#000;border:1px solid #000">
<b>Some Text</b></div><span>This is an other text</span>';
echo str_replace(array("<",">"),array("[","]"),$html);

http://codepad.org/kjKVCzjw

输出

[div style="background-color:#000;border:1px solid #000"]
[b]Some Text[/b][/div][span]This is an other text[/span]

您可以使用str_replace:

$html = str_replace(array('<', '>'), array('[', ']'), $html);

您只需要用[替换<,用]替换>。只需使用str_replace()

$newString = str_replace( "<", "[", str_replace(">", "]", $StringInput) );

最新更新