![](/img/trans.png)
[英]Ho to acces a latin1-encoded database from utf8-encoded PHP using PEAR DB
[英]PHP - strpos() on UTF8-encoded json?
我有 JSON 输出Kirchhellener Stra\ße
.. \ß
是一个 UTF8 编码的德语字母ß
..我想在输出中搜索单词Straße
并替换它,但我不知道怎么做?
$street = json_decode($bit->long_name); // here the `Kirchhellener Stra\u00dfe` is put into $street
$check_strasse1 = strpos($street, 'Straße');
$check_strasse2 = strpos($street, 'Stra\u00dfe');
if($check_strasse1 !== false || $check_strasse2 !== false) {
echo "oki";
$street = str_replace('Strasse', 'Str.', $street);
$street = str_replace('Stra\u00dfe', 'Str.', $street);
} else {
echo "nope";
}
我怎么能检测到$street
有Straße
?
要在 PHP 中使用 UTF-8 编码的字符串,您必须使用mbstring
扩展名。 在这种情况下,您要查找的函数是mb_strpos()
。
或者,在这种情况下,您可以只使用preg_replace()
:
preg_replace('/Straße/u', 'Str.', $street);
u
修饰符确保在匹配字符串时使用 UTF-8 编码。 您不需要检查字符串是否存在,因为如果不存在,它将不会被替换。 您也可以跳过\ß
变体,因为json_decode()
会将此文字转换为正确的 UTF-8 字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.