preg_replace not ignoring [] from string - php

This is my code :
$string = "Hello this is my text [readmore] and this is remaining text";
echo preg_replace("[readmore]","",$string);
This is my expected output :
Hello this is my text and this is remaining text
This is my actual output :
Hello this is my text [] and this is remaining text
Question is simple how to get ride of "[]" too ?

You need to escape [ & ]. Try below regexp,
preg_replace("/\[([^\[\]]++|(?R))*+\]/", "", $string);
OUTPUT:
Hello this is my text and this is remaining text
CodePad Demo.

You need to escape the brackets for use in regular expressions. Also, you might want to look at the manual for preg_replace, since you left off the / that needs to be around your regex.
$string = "Hello this is my text [readmore] and this is remaining text";
echo preg_replace("/\[readmore\]/","",$string);

Escape the '[' and ']' characters:
This script:
$string = "Hello this is my text [readmore] and this is remaining text";
echo preg_replace("[\\[readmore\\] ]","",$string);
This result (I tested this):
Hello this is my text and this is remaining text

Use str_replace
$string = "Hello this is my text [readmore] and this is remaining text";
echo str_replace("[readmore]","",$string);
Output
Hello this is my text and this is remaining text

Related

PHP remove string enclosed a parenthesis and after it

I want to remove the string enclosed with parethesis and the other string on its right
Input:
Hey (Jude) Hello
Expected Output:
Hey
I can only achieve:
Hey Hello by using this code
$string = "Hey (Jude) Hello";
echo preg_replace("/\([^)]+\)/","",$string);
Any thoughts will be appreciated
$string = "Hey (Jude) Hello";
echo preg_replace("/\([^)]+\). */","",$string]);
The dot(.) Matches any character
The star(*) matches 0 or more of preceding character (aka the dot)
Check here for working example
https://regexr.com/3mb6e
You can try
$string = "Hey (Jude) Hello";
echo preg_replace("/\([^)]+\).+/","",$string);

PHP preg_match & preg_replace outputing wrong

<?php
$string = "[img image:left]1.jpg[/img]Example Text 1[img image:left]2.jpg[/img] Example Text 2";
preg_match("/\[img\s*[^>]+\s*\](.*?)\[\/\s*img\]/i", $string, $match);
$result = preg_replace("/\[img\s*[^>]+\s*\](.*?)\[\/\s*img\]/i", $match['1'], $string);
echo $result;
?>
When using this code it should output 1.jpg, Example Text 1, 2.jpg, Example Text 2.
But however it shows only 2.jpg, Example Text 2.
I dont know what i'm doing wrong.
There are two fundamental issues:
you don't need to use a preg_match() and a preg_replace(), you can just use preg_replace() and reference your capture groups in the substitution
it looks like you copy pasted some code from HTML regex, and have [^>]+ inside of your [img], which says 1+ non-> characters..it should really be [^\]]+, 1+ non-] characters
Final solution:
$string = "[img image:left]1.jpg[/img]Example Text 1[img image:left]2.jpg[/img] Example Text 2";
$string = preg_replace("/\[img\s*[^\]]+\s*\](.*?)\[\/\s*img\]/i", ' \1 ', $string);
Demo: RegEx and PHP

Replace exact content or tag

I think that to replace exactly one tag, the best is to use preg_replace. I tried str_replace and replaced all characters. For example if I want replace the two PHP tags in <?php print "Hello parents"; ?> by using str_replace I do this :
$char1=array("<?php","?>");
$char_2=array("","");
str_replace($char1,$char2,$content);
The result will be this :
Hello parents
str_replace "replace all chars"
How can I do the same thing with preg_replace? Because I don't understand how to put chars or separators
Here is what I tried :
$content="<?php Hello parents?>"
$chars='/(\<?php),(?>)/i';
$sus="";
echo preg_replace($chars, $sus, $content);
$content="<?php Hello parents?>"
$chars = "/(<\?php|\?>)/i";
$sus="";
echo preg_replace($chars, $sus, $content);
You can use the | separator to say replace <?php or ?>
And the ? character needs to be escaped as within the RegEx it means 0 or 1 occurances

Regular Expressions inline options

I have a text that has the possible values already in the text, i want to show the right values in situations. I'm not really good with regexes and i don't really know how to explain my problem so here is an example. I've got it working almost:
$string = "This [was a|is the] test!";
preg_replace('/\[(.*)\|(.*)\]/', '$1', $string);
// results in "This was a text!"
preg_replace('/\[(.*)\|(.*)\]/', '$2', $string);
// results in "This is the test!"
This works without problems but when there are two parts it doesn't work anymore because it gets the end bracket from the last.
$string = "This [was a|is the] so this is [bullshit|filler] text";
preg_replace('/\[(.*)\|(.*)\]/', '$1', $string);
//results in "This was a|is the] test so this is [bullshit text"
preg_replace('/\[(.*)\|(.*)\]/', '$2', $string);
//results in "This filler text"
Situation 1 should be the values between ( and | and situation 2 should show the values between | and ).
Your probem is the regex greediness. Add a ? after .* to make it consume only the string within the square brackets:
preg_replace('/\[(.*?)\|(.*?)\]/', '$1', $string);
Likewise could you use the /U ungreedy modifier. Better yet use a more specific match in place of .*? anything.
Instead of using:
(.*)
...to match the stuff inside of the options groups, use this:
([^|\]]*)
That pattern matches anything that is not a | or a ], repeatedly.
You can forbid | characters in your .* replacing the . with [^|] (which means “no |”).
$string = "This [was a|is the] so this is [bullshit|filler] text";
echo preg_replace('/\[([^|]*)\|([^|]*)\]/', '$1', $string);
// results in "This was a so this is bullshit text"
echo '<br />';
echo preg_replace('/\[([^|]*)\|([^|]*)\]/', '$2', $string);
// results in "This is the so this is filler text"

Replacing deprecated ereg_replace() with preg_replace() did not work

The following PHP program replaces the symbols !£$%^& with nulls
<?php
$string = "This is some text and numbers 12345 and symbols !£$%^&";
$new_string = ereg_replace("[^A-Za-z0-9 (),.]", "", $string);
echo "Old string is: ".$string."<br />New string is: ".$new_string;
?>
Output:
Old string is: This is some text and numbers 12345 and symbols !£$%^&
New string is: This is some text and numbers 12345 and symbols
But, I have learned that the function ereg_replace() has been deprecated and that I should use the function preg_replace() instead. I made the substitution like so:
<?php
$string = "This is some text and numbers 12345 and symbols !£$%^&";
$new_string = preg_replace("[^A-Za-z0-9 (),.]", "", $string);
echo "Old string is: ".$string."<br />New string is: ".$new_string;
?>
but got the wrong output:
Old string is: This is some text and numbers 12345 and symbols !£$%^&
New string is: This is some text and numbers 12345 and symbols !£$%^&
What did I do wrong? How do I fix it?
You seem to be missing markers around the regular expression. Try this instead (note the slashes around the pattern).
$string = "This is some text and numbers 12345 and symbols !$%^&";
$new_string = preg_replace("/[^A-Za-z0-9 (),.]/", "", $string);
echo "Old string is: ".$string."<br />New string is: ".$new_string;
You can use any character for the markers as long as the same one is found on both sides. Very useful if your pattern is matching / characters. So this is also valid:
$string = "This is some text and numbers 12345 and symbols !$%^&";
$new_string = preg_replace("~[^A-Za-z0-9 (),.]~", "", $string);
echo "Old string is: ".$string."<br />New string is: ".$new_string;
This is a weird bug that I have experienced as well. for some reason the empty quotes screw up this function but I got it to work by using
preg_replace($pattern, NULL, $string);
instead of
preg_replace($pattern, "", $string);

Categories