GuitarGod Posted December 20, 2007 Share Posted December 20, 2007 Hello, I'm working on some sort of BB code system, and I'm having a little bit of trouble with the colouring on font. [color=??????][/color] I need preg_match to check if a string has the code above, and, more importantly, return the value of ??*??*?? (without the *) Any help is, as always, much appreciated. Link to comment https://forums.phpfreaks.com/topic/82520-solved-using-preg_match-to-get-a-value/ Share on other sites More sharing options...
GuitarGod Posted December 20, 2007 Author Share Posted December 20, 2007 Ok, i figured part of it out: if ( preg_match( '/\[color\=(.*?)\](.*?)\[\/color\]/is', $entry, $matches ) ) When i use 'print_r( $matches )', it works great. But if I used $match[0][0], it only returns the first character of what I want. Any help? Link to comment https://forums.phpfreaks.com/topic/82520-solved-using-preg_match-to-get-a-value/#findComment-419633 Share on other sites More sharing options...
MadTechie Posted December 20, 2007 Share Posted December 20, 2007 <?php $entry = "[color=AABBCCDD]The Text[/color]"; if ( preg_match( '/\[color\=(.*?)\](.*?)\[\/color\]/i', $entry, $matches ) ) { echo "color: ".$matches[1]; //Color echo "<br>text: ".$matches[2]; //text } ?> Link to comment https://forums.phpfreaks.com/topic/82520-solved-using-preg_match-to-get-a-value/#findComment-419643 Share on other sites More sharing options...
GuitarGod Posted December 20, 2007 Author Share Posted December 20, 2007 That works to some extent, but if I have [color=000000]some text[/color] and [color=FF0000]other text[/color] It will only handle the first color, how do I make it handle more? Link to comment https://forums.phpfreaks.com/topic/82520-solved-using-preg_match-to-get-a-value/#findComment-419647 Share on other sites More sharing options...
MadTechie Posted December 20, 2007 Share Posted December 20, 2007 ahh use preg_match_all <?php $entry = "[color=000000]some text[/color] and [color=FF0000]other text[/color]"; preg_match_all('%\[color\=(.*?)\](.*?)\[\/color\]%si', $entry, $result, PREG_SET_ORDER); foreach($result as $r) { echo "color: ".$r[1]; //Color echo "<br>text: ".$r[2]; //text echo "<br>---<br>"; } ?> Link to comment https://forums.phpfreaks.com/topic/82520-solved-using-preg_match-to-get-a-value/#findComment-419653 Share on other sites More sharing options...
GuitarGod Posted December 20, 2007 Author Share Posted December 20, 2007 Thanks man worked like a treat! * solved * Link to comment https://forums.phpfreaks.com/topic/82520-solved-using-preg_match-to-get-a-value/#findComment-419660 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.