regex with str_replace?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

regex with str_replace?

Post 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';
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: regex with str_replace?

Post by AbraCadaver »

You would need preg_replace() and it would be something like this:

Code: Select all

$something = preg_replace('/\[quote[^\]]*\].*?\[\/quote\]/', '', $_POST['something']);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: regex with str_replace?

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: regex with str_replace?

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: regex with str_replace?

Post by manohoo »

Don't you hate when they change things around?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: regex with str_replace?

Post 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']);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: regex with str_replace?

Post 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*?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: regex with str_replace?

Post 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 ?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: regex with str_replace?

Post 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! :)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: regex with str_replace?

Post 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]+/'
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: regex with str_replace?

Post 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!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: regex with str_replace?

Post 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);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: regex with str_replace?

Post by JKM »

Thanks a bunch!
Post Reply