umarsa Posted February 16, 2009 Share Posted February 16, 2009 Hello there, I am making a little script and i need your help because im not very good at regex and these sorts of things. I have the following code: function process_bbcode($text) { // ITALICS, BOLD, LINK, UNDERLINE $string = $text; $bb_replace = array ('/(\[[bb]\])(.+)(\[\/[bb]\])/','/(\[[ii]\])(.+)(\[\/[ii]\])/','/(\[[uu]\])(.+)(\[\/[uu]\])/','/(\[url=)(.+)(\])(.+)(\[\/url\])/'); $bb_replacements = array ('<b>\\2</b>','<i>\\2</i>','<u>\\2</u>','<a href="\\2">Link</a>'); //$bb_replacements = array ('<b>\\2</b>','<a href="\\2">\\4</a>'); $string = preg_replace($bb_replace, $bb_replacements, $string); return $string; } echo process_bbcode("This is [u][i][b]cool[/b][/i][/u] - [url=http://www.google.com].[/url] [img=4567436]"); This script works perfect for underlining, bold, italics and links. The problem is that i want to be able to pass the code that the user enters between IMG tags to another function. So when a user types: [img=4567436] I want my script to extract "4567436" and send it off to another function: get_image("4567436"); Is there anyway that i can do this? Quote Link to comment https://forums.phpfreaks.com/topic/145433-solved-bbcode-img/ Share on other sites More sharing options...
omfgthezerg Posted February 16, 2009 Share Posted February 16, 2009 use the "e" modifier. Quote Link to comment https://forums.phpfreaks.com/topic/145433-solved-bbcode-img/#findComment-763483 Share on other sites More sharing options...
umarsa Posted February 16, 2009 Author Share Posted February 16, 2009 And how exactly would i do that? Can you give a example? Quote Link to comment https://forums.phpfreaks.com/topic/145433-solved-bbcode-img/#findComment-763492 Share on other sites More sharing options...
nrg_alpha Posted February 16, 2009 Share Posted February 16, 2009 The problem is that i want to be able to pass the code that the user enters between IMG tags to another function. One example to your solution could be: function imageText($text){ echo 'I am ' . $text . ', the text between the bbc [img]http://tags.' . "<br />\n"; } function process_bbcode($text) { // ITALICS, BOLD, LINK, UNDERLINE $string = $text; $bb_replace = array ('#\[b\](.+?)\[/b\]#i','#\[i\](.+?)\[/i\]#i','#\[u\](.+?)\[/u\]#i','#\[url=http://([^]]+)\].+?\[/url\]#si'); $bb_replacements = array ('<b>$1</b>','<i>$1</i>','<u>$1</u>','<a href="$1">Link</a>'); $string = preg_replace($bb_replace, $bb_replacements, $string); if( preg_match('#\[img\]([^]]+)\[/img\]#i', $string, $match) ){ imageText($match[1]); } return $string; } echo process_bbcode("This is [u][i][b]cool[/b][/i][/u] - [url=http://www.google.com]google[/url] [img=4567436]"); Output: I am 4567436, the text between the bbc [img] tags. This is cool - Link [img=4567436] Note.. in your initial regex, you had captures that weren't necessary, so I made some changes there.... I also added the i and s modifiers.. i is case insensitive, and s if for the dot match all to include newlines (if the situation calls for it). Quote Link to comment https://forums.phpfreaks.com/topic/145433-solved-bbcode-img/#findComment-763498 Share on other sites More sharing options...
umarsa Posted February 16, 2009 Author Share Posted February 16, 2009 Thanks a lot but, if there are two image tags then it only parses one of them. function imageText($text){ echo 'PIC:'.$text; } function process_bbcode($text) { $string = $text; //$bb_replace = array ('#\[b\](.+?)\[/b\]#i','#\[i\](.+?)\[/i\]#i','#\[u\](.+?)\[/u\]#i','#\[url=http://([^]]+)\].+?\[/url\]#si'); $bb_replace = array ('#\[b\](.+?)\[/b\]#i','#\[i\](.+?)\[/i\]#i','#\[u\](.+?)\[/u\]#i','#\[url=([^]]+)\].+?\[/url\]#si'); $bb_replacements = array ('<b>$1</b>','<i>$1</i>','<u>$1</u>','<a href="$1">Link</a>'); $string = preg_replace($bb_replace, $bb_replacements, $string); if( preg_match('#\[img\]([^]]+)\[/img\]#i', $string, $match) ){ imageText($match[1]); } return $string; } echo process_bbcode("This is [u][i][b]cool[/b][/i][/u] - [url=http://www.google.com]google[/url] [img=4567436] [img=4567436897]"); Output: PIC:4567436This is cool - Link [img=4567436] [img=4567436897] Quote Link to comment https://forums.phpfreaks.com/topic/145433-solved-bbcode-img/#findComment-763553 Share on other sites More sharing options...
nrg_alpha Posted February 16, 2009 Share Posted February 16, 2009 Thanks a lot but, if there are two image tags then it only parses one of them. change the if statement to: if( preg_match_all('#\[img\]([^]]+)\[/img\]#i', $string, $match) ){ imageText($match[1]); } and change the function imageText to: function imageText($text){ foreach($text as $val){ echo 'PIC:'.$val; } } Quote Link to comment https://forums.phpfreaks.com/topic/145433-solved-bbcode-img/#findComment-763560 Share on other sites More sharing options...
umarsa Posted February 16, 2009 Author Share Posted February 16, 2009 And is there a way to do the same for the bold, italics, url and underline? Quote Link to comment https://forums.phpfreaks.com/topic/145433-solved-bbcode-img/#findComment-763563 Share on other sites More sharing options...
nrg_alpha Posted February 16, 2009 Share Posted February 16, 2009 preg_replace should already handle that. Quote Link to comment https://forums.phpfreaks.com/topic/145433-solved-bbcode-img/#findComment-763567 Share on other sites More sharing options...
umarsa Posted February 16, 2009 Author Share Posted February 16, 2009 Ahh ok, thanks so much for your help. You are VERY helpful. Quote Link to comment https://forums.phpfreaks.com/topic/145433-solved-bbcode-img/#findComment-763571 Share on other sites More sharing options...
nrg_alpha Posted February 16, 2009 Share Posted February 16, 2009 I would suggest having a look at other posts dealing with bbcode and regex (remember, forum search is your friend). If you are also interested in learning regex, you can have a look at our resources page, as well as this regex tutorial. EDIT - Oops.. that first link doesn't seem to work.. basically, type in 'regex bbcode' in the forum search field at the top (without the single quotes.. just a space between both words). This will give you additional info on parsing bbcode. Quote Link to comment https://forums.phpfreaks.com/topic/145433-solved-bbcode-img/#findComment-763573 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.