Salman Iqbal
Salman Iqbal

Reputation: 442

php str_replace for danish char's

I am removing danish special char's from a string, I have a string like this 1554896020A2å.pdf danish char's are "æ ø å " for removing danish char's I am using str_replace, I successfully remove these two "æ ø" char's but I don't know this one "å" is not removed from the string. thanks for your help in advance.

I have used this to remove danish char's

$patterns = array('å', 'æ', 'ø');
$replacements = array('/x7D', 'X', '/x7C');
echo str_replace($patterns, $replacements, 1554896020A2å.pdf);

Upvotes: 1

Views: 237

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

The a you have in the string is not a single code unit, it is a code point consisting of two code units, \xCC and \x8A.

Thus, you may add this value to your patterns/replacements:

$patterns = array('å', "a\xCC\x8A", "A\xCC\x8A", 'Å', 'æ', 'ø');
$replacements = array('/x7D', '/x7D', '/x7D', '/x7D', 'X', '/x7C');
echo str_replace($patterns, $replacements, '1554896020A2å.pdf');
// => 1554896020A2/x7D.pdf

See the PHP demo

In PHP 7, you may use "a\u{030A}" / "A\u{030A}" to match these a letters with their diacritic symbol.

Note that you may use /a\p{M}+/ui regex pattern with preg_replace if you decide to go with regex and match any as followed with diacritic marks. i is for case insensitive matching, remove if not needed.

Upvotes: 1

Related Questions