3raser Posted June 3, 2012 Share Posted June 3, 2012 Hia. I want to ask a quick question: Should I be using preg_match or preg_match_all for the following function? Since PHP doesn't support the global & multi-line flag in expressions, my code fails (the if statement) and it returns nope.avi. I know preg_match_all can use globals, but the third argument is a bit confusing. function add_quotes($string, $username) { if(acc_status($username) == 3) { if(preg_match('/\[quote=\w+\]/', $string) && preg_match('/\[\/quote\]/', $string)) { } else { $string.= '<br/><br/><br/><span style="color:red">NOPE.AVI</span>'; } } return $string; } Quote Link to comment https://forums.phpfreaks.com/topic/263575-incorrect-usage-of-preg_match/ Share on other sites More sharing options...
requinix Posted June 3, 2012 Share Posted June 3, 2012 preg_match_all() basically is the /g flag. Unfortunately I'm too tired to be able to explain it right so that's the best sentence I've come up with yet. Meanwhile /g and /m have nothing to do with your problem. Global only matters when you're trying to get the matches themselves (you're just testing that one exists) and multiline mode only affects end- and beginning-of-line anchors (you don't use any). What is the exact value of $string? Quote Link to comment https://forums.phpfreaks.com/topic/263575-incorrect-usage-of-preg_match/#findComment-1350795 Share on other sites More sharing options...
3raser Posted June 3, 2012 Author Share Posted June 3, 2012 Thanks, that clears quite a bit up. I read more on the preg_replace documentary and I noticed the use of groups. Yet for some reason the following doesn't work: function add_quotes($string, $username) { if(acc_status($username) == 3) { //convert QUOTE BBcode to actual HTML format $string = preg_replace('/\[quote\=(.+?)](.+?)\[\/quote\]/s', "$1 said $2", $code_treated); } return $string; } $string = test I wanted to test the groups, but for some reason I can't get them the replace properly. It just replaces the entire $string with a blank message. EDIT: Never mind! I made simple errors in the code above. Solved! Quote Link to comment https://forums.phpfreaks.com/topic/263575-incorrect-usage-of-preg_match/#findComment-1350859 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.