Jump to content

[SOLVED] Tricky Regex Problem


Petsmacker

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/75165-solved-tricky-regex-problem/
Share on other sites

$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.

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>';
?>

 

 

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.