Jump to content

Simple RegEx problem...


Joshua4550

Recommended Posts

str_replace("\[quote.*\].*\[/quote\]", "", $myvar);

 

Basically trying to strip "[ quote=text ]message[ /quote ]" from $myvar (obviously no spaces)

 

Why doesn't it work?

By the way, I'm not a fan of RegEx - and I'm shit with them. I used RegExBuddy but this still doesn't work.

 

Pointers?

Link to comment
Share on other sites

As far as I can tell the only things at fault with the OPs original post was the attempting to use str_replace (which doesn't support REGEX patterns) rather than preg_replace. And the fact that with preg_replace the pattern would require delimiters. Assuming the input could contain more than one quote block however you will also need to make the section between the

tags lazy (which I've done with the question mark). Also making the first catch all match lazy or changing it is required (I replaced it with 'one or more character(s) not ]).

 

$pattern = '#\[quote[^]]*\].*?\[/quote\]#';
$output = preg_replace($pattern, '', $input);

Link to comment
Share on other sites

Probably because your input has newline characters between the

and

tags. By default the . does not match newline characters, if you need it to do so then add the s modifier, you may also wish to add the i modifier to make it case-insensitive, so that

will also match.

 

$pattern = '#\[quote[^]]*\].*?\[/quote\]#is';
$output = preg_replace($pattern, '', $input);

Link to comment
Share on other sites

What makes you say that, works as far as I can tell...

 

$input = '[quote="NAME_HERE"]
QUOTE_HERE[/quote]
Nice quote';
$pattern = '#\[quote[^]]*\].*?\[/quote\]#is';
$output = preg_replace($pattern, '', $input);
echo $output;

Link to comment
Share on other sites

Which is obvious why it didn't work as that output doesn't match the input you mentioned. It has a pairing ID in the opening and closing tag. You never once mentioned this, nor listed it in your example where you claimed it didn't work.

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.