regex with str_replace?
Moderator: General Moderators
regex with str_replace?
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';
$_POST['something'] = 'blabla(quote*)*(/quote)blabla';
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: regex with str_replace?
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.
Re: regex with str_replace?
Abra, I tried your code and it does not work. Regex are prone to errors.
This one does work:
Output:
This is (quote) not (/quote) what I want
This is what I want
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;
?>This is (quote) not (/quote) what I want
This is what I want
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: regex with str_replace?
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.
Re: regex with str_replace?
Don't you hate when they change things around?
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: regex with str_replace?
Yep, so for completeness here's the ( ) way:manohoo wrote:Don't you hate when they change things around?
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.
Re: regex with str_replace?
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 *"e=1*?
One regex question - how can I find *"e=1*?
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: regex with str_replace?
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 ?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 *"e=1*?
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.
Re: regex with str_replace?
The question mark has nothing to do with what I want to find :pAnd I want to remove "e=<number>.
Thanks for the help!
Code: Select all
*text*"e=<number>*text*Thanks for the help!
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: regex with str_replace?
Probably this:JKM wrote:The question mark has nothing to do with what I want to find :pAnd I want to remove "e=<number>.Code: Select all
*text*"e=<number>*text*
Thanks for the help!
Code: Select all
'/"e=[\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.
Re: regex with str_replace?
Hmm, I'm having a problem with the first question. Output = Agree!
But...Output = [quote=blalba] fsa[/quote]Agree!
Code: Select all
<?php
$text = '[quote=blalba]fsa[/quote]Agree!';
$replace = preg_replace('/\[quote[^\]]*\].*?\[\/quote\]/', '', $text);
echo $replace;
?>But...
Code: Select all
<?php
$text = '[quote=blalba]
fsa[/quote]Agree!';
$replace = preg_replace('/\[quote[^\]]*\].*?\[\/quote\]/', '', $text);
echo $replace;
?>- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: regex with str_replace?
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.
Re: regex with str_replace?
Thanks a bunch!