Jump to content

preg_replace problem


ShaolinF

Recommended Posts

Hi Guys,

 

I have the following tag (minus the spaces):

 

[ QUOTE=test ]testes sdfsdf[ /quote ]

 

I have written the following regex which will hopefully get the name 'test' from that tag, see below (minus the space before the quote word):

 

/^\[ quote=(.*)[\]\z]/

 

However, when I specify the replacement string the regex returns the whole tag instead of just the name:

 

<div class="nm">$1</div>

 

$message = preg_replace('/^\[quote=(.*)[\]\z]/', '<div class="nm">$1</div>', $message);

 

returns:

 

<div class="nm">[ QUOTE=test ]testes sdfsdf[ /quote ]</div>

 

Anyone know where Im going wrong ?

 

 

 

.

 

P.S. I have to use preg_replace for this problem.

Link to comment
https://forums.phpfreaks.com/topic/199062-preg_replace-problem/
Share on other sites

Your regular expression is looking for "quote", not "QUOTE". You need to make it case insensitive. Also, using "(.*)" is not efficient. Also, since you also need to replace the ending tag you should just do a search/replace for the name and message.

 

Try this

$message = preg_replace('/^\[quote=([^\]]*)\]([^\[]*)\[\/quote\]/i', "<div class='nm'>$1</div>\n$2", $message);

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.