Petsmacker Posted October 29, 2007 Share Posted October 29, 2007 Basically what I'm trying to do is a mini-PM system in a Forum where people could write a post such as: This is my post [PM=petsmacker]I love you[/PM] Now, everyone else would see 'This is my post' but only I (petsmacker), would see that whoever posted that message loves me. Here is the offending part of my code so far. <?php $patterns = array( '`\[PM=(.+?)\](.+?)\[/PM\]`is', ); $replaces = array( '<strong>PM to \1:</strong><div style="margin:0px 10px;overflow:auto;color:#000000;font-size:8pt;line-height:10pt;padding:5px;background-color:#FFFF99;border:3px solid #A6A6A6;width:93%;"><b>\2</b></div>', ); if ($patterns[0][0] == $realname){ $string = preg_replace($patterns, $replaces , $string); } ?> $realname is the username variable - which works. My main thoughts regard the $patterns[0][0] part, my knowledge of arrays and regex are woeful, how could I get it so that it extracts the username they've written, in order for it to be compared? Quote Link to comment https://forums.phpfreaks.com/topic/75165-solved-tricky-regex-problem/ Share on other sites More sharing options...
kratsg Posted October 29, 2007 Share Posted October 29, 2007 $string = "[PM=petsmacker]I love you[/PM]"; $pattern = "/\[PM=([^\]]*)\]([^\]]*)\[\/PM\]/"; $replace_true = '<strong>PM to '.$1.':</strong><div style="margin:0px 10px;overflow:auto;color:#000000;font-size:8pt;line-height:10pt;padding:5px;background-color:#FFFF99;border:3px solid #A6A6A6;width:93%;"><b>'.$2.'</b></div>'; $replace_false = ''; preg_match_all($pattern,$string,$matches, PREG_SET_ORDER); foreach($matches as $match){ $username = $match[1]; $text = $match[2]; if($realname == $username){ $string = preg_replace($pattern, $replace_true, $string); } else { $string = preg_replace($pattern, $replace_false, $string); } } That should work, probably. Your pattern is a little confusing, so I wrote my own that will work. I am pretty confident about the $matches array. This code will search through the whole text, find anything that matches [PM=username]message[/PM] check to see if it matches the username viewing it, replace correctly, and continue. Quote Link to comment https://forums.phpfreaks.com/topic/75165-solved-tricky-regex-problem/#findComment-380339 Share on other sites More sharing options...
Petsmacker Posted October 29, 2007 Author Share Posted October 29, 2007 Thank you for your help and I can see what you're doing here. However, I get this error: Parse error: syntax error, unexpected T_DNUMBER, expecting T_VARIABLE or '$' in /home/sites/**********/**********/***.php on line 66 Line 66: <?php $replace_true = '<strong>PM to '.$1.':</strong><div style="margin:0px 10px;overflow:auto;color:#000000;font-size:8pt;line-height:10pt;padding:5px;background-color:#FFFF99;border:3px solid #A6A6A6;width:93%;"><b>'.$2.'</b></div>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/75165-solved-tricky-regex-problem/#findComment-380415 Share on other sites More sharing options...
Petsmacker Posted October 29, 2007 Author Share Posted October 29, 2007 Fixed the problem, it was to simply remove the apostrophes and full stops around the variables. $replace_true = '<strong>PM to $1:</strong><div style="margin:0px 10px;overflow:auto;color:#000000;font-size:8pt;line-height:10pt;padding:5px;background-color:#FFFF99;border:3px solid #A6A6A6;width:93%;"><b>$2</b></div>'; Huge thank you kratsg. Quote Link to comment https://forums.phpfreaks.com/topic/75165-solved-tricky-regex-problem/#findComment-380484 Share on other sites More sharing options...
kratsg Posted October 30, 2007 Share Posted October 30, 2007 No problem :-) Quote Link to comment https://forums.phpfreaks.com/topic/75165-solved-tricky-regex-problem/#findComment-380877 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.