How to match one character plus space with PHP preg_replace
Inside my text, I have this output:
Å apat
I want to search for "Å", note the Å + space. and convert it to 'Š'.
So, basically I want to find this letter with a space and replace it with just one letter without a space.
String replace cannot do this and I am not very good with regex, I tried this but it does not work:
$return = preg_replace('/[Å\s]/', 'Š', $return);
Can anyone help me?
NOTE: This word "apat" should not be at the beginning of a sentence, most of the time it is somewhere in the middle.
source to share
$re = "/Å\\s/m";
$str = "Å apat";
$subst = "Š";
$result = preg_replace($re, $subst, $str);
This should do it for you. Watch the demo
source to share
You seem to be trying to solve a coding problem (and probably just a display problem) with string replacement.
This is not the way!
Let's see what happens:
If you look at your hex view string, you find this hex sequence: C5 A0
But depending on the context, this sequence can be interpreted in two different ways:
C5 A0 = Š (C5A0 in utf8) = Å (C5 in unicode) + NO-BREAK SPACE (A0 in Unicode)
So it seems like the problem is that your string is showing up as a sequence of Unicode codes instead of a utf8 encoded string.
I am guessing that the problem comes from a html page that does not have good coding information. Try adding page encoding information between headers:
- html5:
<meta charset="UTF-8"/>
- html4:
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
source to share
No regex needed here, use str_replace
$string = "Å apat";
echo str_replace("Å ", "Š", $string);
//Šapat
Demo:
source to share