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

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);

Related

How to stop preg_replace() returning numerical references?

I'm using preg_replace() to perform a regular expression search and replace.
$string = "This isn't my real string but it is a good example.";
$result = preg_replace($pattern, $replacement, $string);
echo $result; // This isn’t my real string but it is a good example.
As you can see, $string contains a single quote. Regardless of whether a match is found, when I output the return value of preg_replace(), my single quote becomes ’
How can I stop preg_replace() returning numerical references such as ’? I need my string to keep the single quote character.
Update
Here's my pattern:
$pattern = '/#(\w+)/';
Update 2
Here's my replacement string:
$replacement = '#$1';
you can try using html_entity_decode() to achieve that
here is a snippet
$string = "This isn't my real string but it is a good example.";
$result = preg_replace($pattern, $replacement, $string);
echo html_entity_decode($result); // This isn't my real string but it is a good example.

remove all characters after a character using regular expression in php

I need to remove all the characters after the character which I select in a string.
Here is my string
$string = "Blow the fun in the sun.paste a random text";
$new_string = preg_replace("/paste.*/","",$string,-1,$count);
echo $new_string;
My output is Blow the fun in the sun.
If my string is like this
$string = "Blow the fun in the sun.paste \n a random text";
$new_string = preg_replace("/paste.*/","",$string,-1,$count);
echo $new_string;
My output is
Blow the fun in the sun.
a random text
But, I need my output as Blow the fun in the sun. even if there are \n or \t or some other special characters in my strings. How can I match this, while taking those special characters into consideration?
You will need s flag (DOTALL) to make DOT match new lines:
$new_string = preg_replace("/paste.*/"s, "", $string, -1, $count);
Without s flag your regex is not matching new lines as your input contains new lines and you want to replace string that contains new line as well.

preg_replace not ignoring [] from string

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

preg_replace, string and numeric replacement

So this is a preg_replace associated question i guess,
I have a string with multiple repeating patterns
they all formated as:
some string :22: more text :12: etc
how do i replace the ":" around them with some different char?
You can do something like this:
$string = 'some string :22: more text :12: etc';
$regex = '/:(\d+):/';
$newString = preg_replace($regex, "#$1#", $string);
Note: You have to replace the '#' in the second parameter with the char you want (also different chars before and after the numbers).
Sbustitudes _ for : around numbers:
preg_replace('/:(\d+):/', '_$1_', 'some string :22: more text :12: etc');
EDIT: Misunderstood original question. However, is still a flexible option:
$result = str_replace(":22:", "tag", "some string :22: more text :12: etc");
$result = str_replace(":12:", "other_tag", $result);
Replace the ? character with your replacement character.

How do I compress space characters with PHP regular expressions?

$string = 'Hello this is a bunch of numbers: 333 and letters and $pecial Characters#!*(';
$foo = preg_replace("/[^a-zA-Z0-9\s]/", "", $string);
echo $foo;
The above returns:
Hello this is a bunch of numbers 333 and letters and pecial Characters
I want to retain spaces but not if theres more than one. How can that be done?
It should look like:
Hello this is a bunch of numbers 333 and letters and pecial Characters
$foo = preg_replace("/[^a-zA-Z0-9\s]/", "", $string);
$foo = preg_replace('/\s+/',' ',$foo);
One regex will do it:
$foo = preg_replace('/[^a-zA-Z0-9\s]|(\s)\s+/', '$1', $string);
I haven't worked with PHP, but in Perl it's something like:
s/\s+/ /g
i.e. replace any sequence of one or more spaces with a single space.
So I imagine the PHP to compress spaces would be:
$foo = preg_replace("/\s{2,}/", " ", $string);
I don't think there should be any problems with running two preg_replace lines, especially if it makes the code clearer.
Replace globally (edit - tested):
/(?<=\s)\s+|[^a-zA-Z0-9\s]+/
with ""

Categories