使用正则表达式替换 PHP 中字符串中的特殊字符(某些字符除外)

发布于 2024-12-29 19:31:02 字数 261 浏览 0 评论 0原文

我想替换字符串中除空格和 - 之外的所有特殊字符。

例如,

Hello-my name *is (Jämes93!

Hello-my name is James93

甚至

Hello-my nme is Jmes93

我有以下内容,但它不起作用。请有人帮忙吗? 谢谢

preg_replace('#[^\w-]#',"",$string)

I would like to replace all special characters in a string except space and -.

For example,

Hello-my näme *is (Jämes93!

to

Hello-my name is James93

or even

Hello-my nme is Jmes93

I have the following but it won't work. Pls can someone help?
Thanks

preg_replace('#[^\w-]#',"",$string)

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

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

发布评论

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

评论(4

纸短情长 2025-01-05 19:31:02

您不想对这些使用正则表达式。使用 iconv() 效果会更好
音译

$result = iconv("UTF-8", "ASCII//TRANSLIT", $text);

假设原始字符串是用 编码的UTF-8 并且您想将其转换为 ASCII 这接近您的问题。

只要您知道原始字符集,就可以以相同的方式与其他字符集进行转换。

顺便说一下,你不能使用 preg_replace 来做到这一点,它不支持 < em>音译

You don't want to use regex for these. You are much better off using iconv()
with transliteration:

$result = iconv("UTF-8", "ASCII//TRANSLIT", $text);

This assumes that original string is encoded in UTF-8 and you want to convert it to ASCII which comes close to your question.

You can convert to/from other charsets the same way just as long as you know the original charset.

By the way, you can't do that with preg_replace, it does not support transliteration.

窗影残 2025-01-05 19:31:02

只是为了回答您的具体问题,您的代码的问题是 preg_replace 实际上并没有更改字符串。您需要将结果分配回 $string。

Just to answer your specific question, the problem with your code is that preg_replace doesn't actually change the string. You need to assign the result back to $string.

断念 2025-01-05 19:31:02

有几种方法可以做到这一点:

第一种方法:

$string = "Hello-my näme *is (Jämes93!";

$chars = array(
        'Š'=>'S', 'š'=>'s', 'Đ'=>'D', 'đ'=>'d', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
        'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
        'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
        'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
        'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
        'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
        'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
        'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r',
    );
echo preg_replace('/[^a-zA-Z0-9 ]/s', '', strtr($string, $chars));

第二种方法:

echo iconv("UTF-8", "ISO-8859-1//TRANSLIT", $string);

但第二种方法不会替换所有特殊字母。我更喜欢你使用第一个。

希望这对您有帮助。

There are several ways to do it:

First method:

$string = "Hello-my näme *is (Jämes93!";

$chars = array(
        'Š'=>'S', 'š'=>'s', 'Đ'=>'D', 'đ'=>'d', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
        'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
        'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
        'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
        'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
        'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
        'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
        'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r',
    );
echo preg_replace('/[^a-zA-Z0-9 ]/s', '', strtr($string, $chars));

Second method:

echo iconv("UTF-8", "ISO-8859-1//TRANSLIT", $string);

But second method will not replace all the special alphabets. I prefer you to use first one.

Hope this helps you.

ˉ厌 2025-01-05 19:31:02

您可以使用如下代码:

var_dump ( preg_replace('~[^A-Za-z\d\s-]+~u', '', ('Hello-my näme *is (Jämes93!')) );

输出:

string(22) "Hello-my nme is Jmes93"

You can use code like this:

var_dump ( preg_replace('~[^A-Za-z\d\s-]+~u', '', ('Hello-my näme *is (Jämes93!')) );

OUTPUT:

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