porton
porton

Reputation: 5803

PHP: Replace a unicode symbol

$str = str_replace ('–', '-', $str); does not work (the longer Unicode dash character is not replaced with minus character, as I want.

$str is read from a database and should be in UTF-8.

PHP code is run from an Apache server.

I need to replace all these long dashes with minus char.


$dash = "–";
echo "string: " . bin2hex($str) . ", dash: " . bin2hex($dash) . "\n";
echo "string: " . $str . ", dash: " . $dash . "\n";

string: 5a656c626f726166202623383231313b20d0bdd0bed0b2d18bd0b920d0bfd180d0b5d0bfd0b0d180d0b0d18220d0b4d0bbd18f20d0bbd0b5d187d0b5d0bdd0b8d18f20d0bcd0b5d0bbd0b0d0bdd0bed0bcd18b, dash: e28093
string: Zelboraf – новый препарат для лечения меланомы, dash: –

What is wrong (not in proper UTF-8): string or dash?

Upvotes: 0

Views: 4887

Answers (3)

porton
porton

Reputation: 5803

It was a HTML entity encoded "–" :-) That's is my failure.

Upvotes: 3

Peon
Peon

Reputation: 8020

<?php

$str = 'Test–asd';

$old = '–';
$new = '!';

$str = str_replace ( $old, $new, $str );

echo $str;

?>

This works just fine for me:

Output:

Test!asd

Seems that you have problems with different encoding, not UTF8 character changes.

Upvotes: 1

Develoger
Develoger

Reputation: 3998

EDIT:

try this:

$str = str_replace('\xe2\x80\x94', '-', $str);

Try:

$str = str_replace(chr(150), '-', $str);    // endash

or

$str = str_replace(chr(151), '-', $str);   // emdash

I think that the second one suits you more.

Upvotes: 0

Related Questions