Reputation: 659
Inside my text I have this output:
Å apat
I want to search for 'Å ', note Å + space. and Convert it to 'Š'.
So basically I want to find this letter followed with space and replace it with just one letter without space.
String replace can not do this, and I am not very good with regex, I have tried this but it does not work:
$return = preg_replace('/[Å\s]/', 'Š', $return);
Can someone help me please ?
NOTE: this word "Å apat" does not have to be at the start of sentence, most of the time it is somewhere in the middle.
Upvotes: 3
Views: 978
Reputation: 89557
It seems you are trying to solve an encoding problem (and probably only a display problem) with a string replacement.
It's not the way to go!
Let's see what happens:
If you looks at your string with an hex viewer you will find this hex sequence: C5 A0
But depending of the context, this sequence can be interpreted in two different ways:
C5 A0 = Š (C5A0 in utf8 encoding) = Å (C5 in unicode) + NO-BREAK SPACE (A0 in unicode)
So it seems that the problem is that your string is displayed as a sequence of unicode code points instead of a utf8 encoded string.
I assume that the problem comes from the html page that hasn't the good encoding information. Try to add information about the page encoding between head tags:
<meta charset="UTF-8"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
Link: chartable with unicode code point and utf8 encoding
Upvotes: 1
Reputation: 98921
No need for a regex here, use str_replace
$string = "Å apat";
echo str_replace("Å ", "Š", $string);
//Šapat
Demo:
Upvotes: 0
Reputation: 67968
$re = "/Å\\s/m";
$str = "Å apat";
$subst = "Š";
$result = preg_replace($re, $subst, $str);
This should do it for you.See demo
Upvotes: 3
Reputation: 4887
This should help you:
$result = preg_replace('/Å /', 'Š', $text);
What your regex [Å\s]
does is:
Match a the character Å or any whitespace character and replace it with Š
Upvotes: 0