Page 1 of 1

regex with str_replace?

Posted: Wed Jan 06, 2010 12:30 pm
by JKM
I want to remove [quote*]*[/quote] from $_POST['something'] (* = any text (it might just be (quote)(/quote), but it could also be (quoteblabla)blalba(/quote))):
$_POST['something'] = 'blabla(quote*)*(/quote)blabla';

Re: regex with str_replace?

Posted: Wed Jan 06, 2010 12:50 pm
by AbraCadaver
You would need preg_replace() and it would be something like this:

Code: Select all

$something = preg_replace('/\[quote[^\]]*\].*?\[\/quote\]/', '', $_POST['something']);

Re: regex with str_replace?

Posted: Wed Jan 06, 2010 1:11 pm
by manohoo
Abra, I tried your code and it does not work. Regex are prone to errors.

This one does work:

Code: Select all

<?php
$text = "This is (quote) not (/quote) what I want";
echo $text;
/* substr_replace(string,replacement,start,length) 
we want to find start and length, replacement is ""*/
 
// find the position of (quote)
$start = strpos($text,"(quote)"); 
 
// find the position of (/quote)
$length = strpos($text,"(/quote)"); 
 
// now we are ready
 
$text1 = substr_replace($text,"",$start,$length);
 
// or in one line:
$text2 = substr_replace($text,"",strpos($text,"(quote)"),strpos($text,"(/quote)"));
echo "<br />".$text2;
?>
Output:
This is (quote) not (/quote) what I want
This is what I want

Re: regex with str_replace?

Posted: Wed Jan 06, 2010 1:18 pm
by AbraCadaver
I tested mine and it works fine. The issue is that the OP used two separate structures in his post. He started off with [quote] [/quote] and then started using (quote) (/quote). I used the [quote] way as that is what I assumed that he meant.

Re: regex with str_replace?

Posted: Wed Jan 06, 2010 1:23 pm
by manohoo
Don't you hate when they change things around?

Re: regex with str_replace?

Posted: Wed Jan 06, 2010 1:26 pm
by AbraCadaver
manohoo wrote:Don't you hate when they change things around?
Yep, so for completeness here's the ( ) way:

Code: Select all

$something = preg_replace('/\(quote[^\)]*\).*?\(\/quote\)/', '', $_POST['something']);

Re: regex with str_replace?

Posted: Wed Jan 06, 2010 1:57 pm
by JKM
Oh sorry (and thanks!), I had to change it to (), so it didn't get formatted into a quote.

One regex question - how can I find *&quote=1*?

Re: regex with str_replace?

Posted: Wed Jan 06, 2010 2:00 pm
by AbraCadaver
JKM wrote:Oh sorry (and thanks!), I had to change it to (), so it didn't get formatted into a quote.

One regex question - how can I find *&quote=1*?
Depends. Explain that more. Is the * a literal * or it means wildcard anything? How about the ?, what does it mean or is it a literal ?

Re: regex with str_replace?

Posted: Wed Jan 06, 2010 2:53 pm
by JKM
The question mark has nothing to do with what I want to find :p

Code: Select all

*text*&quote=<number>*text*
And I want to remove &quote=<number>.

Thanks for the help! :)

Re: regex with str_replace?

Posted: Wed Jan 06, 2010 2:58 pm
by AbraCadaver
JKM wrote:The question mark has nothing to do with what I want to find :p

Code: Select all

*text*&quote=<number>*text*
And I want to remove &quote=<number>.

Thanks for the help! :)
Probably this:

Code: Select all

'/&quote=[\d]+/'

Re: regex with str_replace?

Posted: Wed Jan 06, 2010 3:43 pm
by JKM
Hmm, I'm having a problem with the first question.

Code: Select all

<?php
 
$text = '[quote=blalba]fsa[/quote]Agree!';
$replace = preg_replace('/\[quote[^\]]*\].*?\[\/quote\]/', '', $text);
echo $replace;
 
?>
Output = Agree!

But...

Code: Select all

<?php
 
$text = '[quote=blalba]
fsa[/quote]Agree!';
$replace = preg_replace('/\[quote[^\]]*\].*?\[\/quote\]/', '', $text);
echo $replace;
 
?>
Output = [quote=blalba] fsa[/quote]Agree!

Re: regex with str_replace?

Posted: Wed Jan 06, 2010 4:51 pm
by AbraCadaver
Doh! I should have thought of that. The s modifier will let it match the newline and I added the i modifier in case you want it case insensitive QUOTE, Quote, etc:

Code: Select all

$replace = preg_replace('/\[quote[^\]]*\].*?\[\/quote\]/si', '', $text);

Re: regex with str_replace?

Posted: Wed Jan 06, 2010 7:07 pm
by JKM
Thanks a bunch!