gpt4 book ai didi

PHP preg_replace oddity with £ pound sign and ã

转载 作者:可可西里 更新时间:2023-10-31 22:49:17 26 4
gpt4 key购买 nike

我正在应用以下功能

<?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 éèêëñòóôõöàáâäåìíîïùúûüýÿã";

它与英镑符号 £ 冲突,将英镑符号替换为黑色方 block 中未识别的问号。

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

谢谢,

巴里

更新:谢谢大家。更改函数添加 u 修饰符:pt2.php.net/manual/en/… – 正如 Artefacto 所建议的那样,这是一种享受

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

最佳答案

如果您的字符串是 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);

关于PHP preg_replace oddity with £ pound sign and ã,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2903595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com