-3

I have an html string

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

I want to convert it to this

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

I have searched a lot on google to get some php script to convert html to bbcode but could not find. I don't know the regex. If you give me some idea, with example code, It will give me the startup.

If it could be done with some other php function, please suggest me that.

Munib
  • 3,533
  • 9
  • 29
  • 37

3 Answers3

3

use this

$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

output

[div style="background-color:#000;border:1px solid #000"]
[b]Some Text[/b][/div][span]This is an other text[/span]
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
0

You could use str_replace:

$html = str_replace(array('<', '>'), array('[', ']'), $html);
billyonecan
  • 20,090
  • 8
  • 42
  • 64
0

All you need is replace < with [ and > with ]. Simply use str_replace().

$newString = str_replace( "<", "[", str_replace(">", "]", $StringInput) );
hjpotter92
  • 78,589
  • 36
  • 144
  • 183