-3

我有一个 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函数来完成,请建议我。

4

3 回答 3

3

用这个

$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]
于 2013-03-28T12:27:46.760 回答
0

你可以使用str_replace

$html = str_replace(array('<', '>'), array('[', ']'), $html);
于 2013-03-28T12:28:04.940 回答
0

您所需要的只是<[>替换]。只需使用str_replace().

$newString = str_replace( "<", "[", str_replace(">", "]", $StringInput) );
于 2013-03-28T12:28:08.133 回答