Guest

Untitled

By: a guest on Sep 13th, 2008  |  syntax: PHP  |  size: 0.60 KB  |  hits: 261  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. <?php
  2. $text = '1] Good: [b]Bold[/b][i]Italic[/i]
  3. 2] Good: [b][i]Both[/i][/b]
  4. 3] Bad: [b][i]Alternating tags[/b][/i]
  5. 4] Bad: [b][i]Misplaced tag[/b]
  6. 5] Good: [b][i][u][s]All tags[/s][/u][/i][/b]
  7. 6] Bad: [b][i][u][s]Swapped end tags[/s][/i][/u][/b]';
  8. echo "<pre>$text<hr>";
  9. // replace good tag pairs
  10. while(preg_match('#\[([bisu])\]((?:(?!\[/?[bisu]\]).)*)\[/\1\]#is',$text)){
  11.   $text=preg_replace('#\[([bisu])\]((?:(?!\[/?[bisu]\]).)*)\[/\1\]#is','<\1>\2</\1>',$text);
  12. }
  13. // replace bad tags
  14. $text=preg_replace('#\[/?[bisu]\]#i','',$text);
  15. // htmlencode to display example
  16. echo htmlentities($text);
  17. ?>