Nagarajan Perumal Nagarajan Perumal - 4 months ago 6
PHP Question

Remove two characters all occurrence in a string using a pattern

$str = "XXXX, P.a and XXXXXX, M.a and XXXXX, V.c and XXXXXX, M.w and XXXXX, I.b and XXXXá";


In the above string, I want to remove '.' and next alphabet character(a-z) in all occurrence. I tried the below preg_replace. But it removes all lower case characters :(

echo $str = preg_replace("/.[a-z]/u","", $str);


I need output

XXXX, P and XXXXXX, M and XXXXX, V and XXXXXX, M and XXXXX, I and XXXXá


How to solve this one?

Answer

Try this:

echo $str = preg_replace("/\\.[a-z]/u","", $str); 

You need to escape the ., since that means "any character" in a regular expression.

Output:

XXXX, P and XXXXXX, M and XXXXX, V and XXXXXX, M and XXXXX, I and XXXXá

Comments