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
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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.