Joshua4550 Posted July 9, 2010 Share Posted July 9, 2010 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? Quote Link to comment Share on other sites More sharing options...
bh Posted July 9, 2010 Share Posted July 9, 2010 Hi, use the preg_replace function Quote Link to comment Share on other sites More sharing options...
Joshua4550 Posted July 9, 2010 Author Share Posted July 9, 2010 Thanks. Although, now my RegEx doesn't work (I expected this lol) Something about backslashes? Quote Link to comment Share on other sites More sharing options...
bh Posted July 9, 2010 Share Posted July 9, 2010 Sg like this? $my_var = 'abc efg[quote="text"]message[/quote]hij klm'; echo preg_replace('/(.*)\[quote.*\\quote\](.*)/', '$1 $2', $my_var); Quote Link to comment Share on other sites More sharing options...
cags Posted July 9, 2010 Share Posted July 9, 2010 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); Quote Link to comment Share on other sites More sharing options...
Joshua4550 Posted July 9, 2010 Author Share Posted July 9, 2010 Thanks a LOT. Although, neither of the RegEx worked? :S Quote Link to comment Share on other sites More sharing options...
cags Posted July 9, 2010 Share Posted July 9, 2010 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); Quote Link to comment Share on other sites More sharing options...
Joshua4550 Posted July 9, 2010 Author Share Posted July 9, 2010 Thanks for the pointers for skipline and case insensitive I used the pattern on this: [quote="NAME_HERE"] QUOTE_HERE[/quote] Nice quote But apparently, this did not match? Quote Link to comment Share on other sites More sharing options...
cags Posted July 9, 2010 Share Posted July 9, 2010 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; Quote Link to comment Share on other sites More sharing options...
Joshua4550 Posted July 9, 2010 Author Share Posted July 9, 2010 Well I used that expression, and it didn't work. I made it print the results to: [quote="You"]Hello[/quote] Lol hi The result was: [quote="You":2dxs0gq9]Hello[/quote:2dxs0gq9] Lol hi Quote Link to comment Share on other sites More sharing options...
Joshua4550 Posted July 9, 2010 Author Share Posted July 9, 2010 D.w I resolved it. Used part of your regex though, so thanks. Quote Link to comment Share on other sites More sharing options...
cags Posted July 9, 2010 Share Posted July 9, 2010 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.