Jump to content

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
https://forums.phpfreaks.com/topic/207218-simple-regex-problem/
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);

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

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.