PHP preg_replace 奇怪的内容为 £英镑符号和 ã

发布于 2024-09-02 20:19:49 字数 974 浏览 4 评论 0原文

我正在应用以下函数

<?php

function replaceChar($string){
    $new_string = preg_replace("/[^a-zA-Z0-9\sçéèêëñòóôõöàáâäåìíîïùúûüýÿ]/", "", $string);
    return $new_string;
}

$string = "This is some text and numbers 12345 and symbols !£%^#&$ and foreign letters éèêëñòóôõöàáâäåìíîïùúûüýÿ";

echo replaceChar($string);
?>

,该函数工作正常,但如果我将 ã 添加到 preg_replace 中,就像

$new_string = preg_replace("/[^a-zA-Z0-9\sçéèêëñòóôõöàáâãäåìíîïùúûüýÿ]/", "", $string);

$string = "This is some text and numbers 12345 and symbols !£%^#&$ and foreign letters éèêëñòóôõöàáâäåìíîïùúûüýÿã";

它与英镑符号 £ 冲突,并将英镑符号替换为黑色方块中的不明问号。

这并不重要,但有人知道这是为什么吗?

谢谢巴里

更新

:谢谢大家。更改了添加 u 修饰符的函数:pt2.php.net/manual/en/… – 按照 Artefacto 的建议,效果很好

function replaceChar($string){
$new_string = preg_replace("/[^a-zA-Z0-9\sçéèêëñòóôõøöàáâãäåìíîïùúûüýÿ]/u", "", $string);
return $new_string;
}

I am applying the following function

<?php

function replaceChar($string){
    $new_string = preg_replace("/[^a-zA-Z0-9\sçéèêëñòóôõöàáâäåìíîïùúûüýÿ]/", "", $string);
    return $new_string;
}

$string = "This is some text and numbers 12345 and symbols !£%^#&$ and foreign letters éèêëñòóôõöàáâäåìíîïùúûüýÿ";

echo replaceChar($string);
?>

which works fine but if I add ã to the preg_replace like

$new_string = preg_replace("/[^a-zA-Z0-9\sçéèêëñòóôõöàáâãäåìíîïùúûüýÿ]/", "", $string);

$string = "This is some text and numbers 12345 and symbols !£%^#&$ and foreign letters éèêëñòóôõöàáâäåìíîïùúûüýÿã";

It conflicts with the pound sign £ and replaces the pound sign with the unidentified question mark in black square.

This is not critical but does anyone know why this is?

Thank you,

Barry

UPDATE: Thank you all. Changed functions adding the u modifier: pt2.php.net/manual/en/… – as suggested by Artefacto and works a treat

function replaceChar($string){
$new_string = preg_replace("/[^a-zA-Z0-9\sçéèêëñòóôõøöàáâãäåìíîïùúûüýÿ]/u", "", $string);
return $new_string;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我一直都在从未离去 2024-09-09 20:19:49

如果您的字符串采用 UTF-8 格式,则必须将 u 修饰符添加到正则表达式中。像这样:

function replaceChar($string){
    $new_string = preg_replace("/[^a-zA-Z0-9\sçéèêëñòóôõöàáâäåìíîïùúûüýÿ]/u", "", $string);
    return $new_string;
}

$string = "This is some text and numbers 12345 and symbols !£%^#&$ and foreign letters éèêëñòóôõöàáâäåìíîïùúûüýÿ";

echo replaceChar($string);

If your string is in UTF-8, you must add the u modifier to the regex. Like this:

function replaceChar($string){
    $new_string = preg_replace("/[^a-zA-Z0-9\sçéèêëñòóôõöàáâäåìíîïùúûüýÿ]/u", "", $string);
    return $new_string;
}

$string = "This is some text and numbers 12345 and symbols !£%^#&$ and foreign letters éèêëñòóôõöàáâäåìíîïùúûüýÿ";

echo replaceChar($string);
巷雨优美回忆 2024-09-09 20:19:49

您的字符串很可能是 UTF-8,但 preg_replace() 正在处理字节

Chances are that your string is UTF-8, but preg_replace() is working on bytes

迟月 2024-09-09 20:19:49

该代码是有效的......

也许你应该尝试中欧字符编码

<?php
header ('Content-type: text/html; charset=ISO-8859-2');
?>

that code is valid ...

maybe you should try Central-European character encoding

<?php
header ('Content-type: text/html; charset=ISO-8859-2');
?>

愚人国度 2024-09-09 20:19:49

您可能想看看 mb_ereg_replace()。正如 Mark 提到的,preg_replace 仅适用于字节级别,对于多字节字符编码效果不佳。

干杯,
法比安

You might want to take a look at mb_ereg_replace(). As Mark mentioned preg_replace only works on byte level and does not work well with multibyte character encodings.

Cheers,
Fabian

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文