Parse a String with BB Code or RSS using PHP
Jul.15, 2009 in
PHP
Writing a function to parse BB Code and RSS Feeds each time you need them is a pain. Here’s a use function to throw into an include or just have on hand for later.
A little while ago I was stuck reading forum content outside of the forums. Pulling anything from the database is easy but without a nice set of functions to handle the odd strings that can somehow come out, it can be a little difficult. This function here made everything a lot better. Unless your forums look like this:
This string handling function will do most of work for you once you set the tags. To improve on the function, just make parsed tags dynamic and pass them into the function when calling it.
A little while ago I was stuck reading forum content outside of the forums. Pulling anything from the database is easy but without a nice set of functions to handle the odd strings that can somehow come out, it can be a little difficult. This function here made everything a lot better. Unless your forums look like this:

This string handling function will do most of work for you once you set the tags. To improve on the function, just make parsed tags dynamic and pass them into the function when calling it.
| PHP | | copy code | | ? |
| 01 | |
| 02 | // Setup functions |
| 03 | function get_string_between($string, $start, $end){
|
| 04 | $string = " ".$string; |
| 05 | $ini = strpos($string,$start); |
| 06 | if ($ini == 0) return ""; |
| 07 | $ini += strlen($start); |
| 08 | $len = strpos($string,$end,$ini) - $ini; |
| 09 | return substr($string,$ini,$len); |
| 10 | } |
| 11 | |
| 12 | $fullstring = "this is my [tag]app[/tag]"; |
| 13 | $parsed = get_string_between($fullstring, "[tag]", "[/tag]"); |
| 14 | |
| 15 | echo $parsed; // (result = app) |
| 16 |














July 17th, 2009 at 4:40 AM this is a wonderful tutorial. i read it 2 times and get a fantastic results and sure i put a copy of this lesson on my site.
July 17th, 2009 at 11:52 AM thanks for this code i will try to do it in my site
August 16th, 2009 at 7:10 PM when your have more than one [TAG] in your string it will only parse the first string…
ie.
$fullstring = “this [tag]is[/tag] my [tag]app[/tag]“;
will output “is”
i use for BBCode and RSS feed parsing the php pear packages
HTML_BBCodeParser and XML_RSS found @ http://pear.php.net
Best Regards
nfo
August 17th, 2009 at 2:05 AM A bit more efficient way is to use regular expressions:
preg_match_all(’/\[tag\](.*?)\[\/tag\]/’, $string, $matches);