Sars Posted December 10, 2008 Share Posted December 10, 2008 Hi everyone - first post. Anyywho... I'm currently working on a BBphraser for an upcoming website (similar to Newgrounds.com - ZombieCreations.org/test if you want a sneak peek...). It's all working except for some of my preg match code; this is understandable because my understandind of preg match patterns is limited. Issue Outline: When phrasing something like I like [url=http://www.google.com]Google[/url]. I dislike [url=http://www.yahoo.com]Yahoo[/url]. Both links come out with their href's equaling http://www.google.com - this goes for colour tags, etc. They all are set to the first preg match's...uhh..match. The Code: if(preg_match("^\[url\](.+?)\[/url\]^im",quote_smart($string),$urls)) { $string = preg_replace_callback('^\[url\](.+?)\[/url\]^im',create_function('$urls','return \'<a href="'.trim($urls[1]).'" target="_blank">'.trim($urls[1]).'</a>\';'),$string); } Also - I'm looking for a way to close tags at the end of all code if they're not closed - to keep people from breaking the site. Ideas? And last but not least - PLEASE don't send me to a 'premade BBtags phraser' because I've got alot of custom things going on in mine; and I like to have an indepth understanding of my code where possible (sounds ironic due to my comments eairler in this post, eh?) Thanks, Sars Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted December 10, 2008 Share Posted December 10, 2008 It looks to me like you're overcomplicating the issue. You don't need to search and then replace - you can just use preg_replace. preg_match() only returns the first match - you have to use preg_match_all() to find all matches in a string. I suspect this is causing all of your URLs to be replaced by the first one. Something like this should work: <?php $patterns[] = "|\[url\](.*?)\[/url\]|is"; $patterns[] = "|\[url=(.*?)\](.*?)\[/url\]|is"; $replacements[] = '<a href="$1">$1</a>'; $replacements[] = '<a href="$1">$2</a>'; preg_replace($patterns,$replacements,$string); ?> Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 10, 2008 Share Posted December 10, 2008 yo dude, looks like your database has died on you... Quote Link to comment Share on other sites More sharing options...
Sars Posted December 12, 2008 Author Share Posted December 12, 2008 Thanks for the help - that seems to be working. Any idea how to do something like... if(preg_match("^\[size=(.+?)\](.+?)\[/size\]^im",quote_smart($string),$sizes)) { if ($sizes[1]=='tiny') { $sizes[1]='7pt'; } if ($sizes[1]=='small') { $sizes[1]='8pt'; } if ($sizes[1]=='normal') { $sizes[1]='10pt'; } if ($sizes[1]=='large') { $sizes[1]='14pt'; } if ($sizes[1]=='huge') { $sizes[1]='20pt'; } else { //Error } $string = preg_replace_callback('^\[size=(.+?)\](.+?)\[/size\]^im',create_function('$sizes','return \'<span style="font-size:'.trim($sizes[1]).'">'.trim($sizes[2]).'</span>\';'),$string); } Because I'm not sure how to check what's matched to see if its valid with your method, etc. Either that or show me how to use preg match all and print out the results, at the moment I just get the words replaced with 'Array' and I'm unsure of what I should be passing to get the correct result from the array. Thanks for the help so far! Quote Link to comment 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.