PHP 从字符串中删除符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16733674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:37:29  来源:igfitidea点击:

PHP remove symbols from string

phpstringsymbols

提问by Dimitris Damilos

Searching through the internet and this website as well, I've found several topics on the matter. Thing is, there are countless solutions if the inserted strings must contain only characters of the Latin alphabet, but when the case requires text of other alphabets it gets a bit tricky.

通过互联网和这个网站搜索,我发现了几个关于这个问题的话题。问题是,如果插入的字符串必须只包含拉丁字母的字符,那么有无数的解决方案,但是当案例需要其他字母的文本时,它会变得有点棘手。

Is there any way I can strip in PHP all symbols from a string, but leave the actual letters of all UTF-8 alphabets? I have tried already creating an array of all the characters of my keyboard and then by using str_replace or preg_replace remove them, but then I found out that different countries have also different keyboards sometimes which include different symbols. For example, my qwerty keyboard doesn't have the symbol, which a British keyboard might have.

有什么方法可以在 PHP 中从字符串中删除所有符号,但保留所有 UTF-8 字母表的实际字母?我已经尝试创建一个包含键盘所有字符的数组,然后使用 str_replace 或 preg_replace 删除它们,但后来我发现不同的国家/地区有时也有不同的键盘,其中包含不同的符号。例如,我的 qwerty 键盘没有英式键盘可能有的符号。

I know this is a weird question, I am just wondering if there is an easy solution to it which I may have missed.

我知道这是一个奇怪的问题,我只是想知道是否有我可能错过的简单解决方案。

Any help would be very much appreciated!

任何帮助将不胜感激!

EDIT:OK After some better and extended Google-ing I have found out that the following regular expression works fine for what I need and it keeps all letters of all types of alphabets while removes all symbols. I am sharing it here in case somebody else would need to do the same.

编辑:好的,经过一些更好的和扩展的谷歌搜索后,我发现以下正则表达式可以很好地满足我的需要,它保留所有类型字母的所有字母,同时删除所有符号。我在这里分享它以防其他人需要做同样的事情。

$string = preg_replace('/[^\p{L}\p{N}\s]/u', '', $string);

$string = preg_replace('/[^\p{L}\p{N}\s]/u', '', $string);

回答by Dimitris Damilos

The solution is this: $string = preg_replace('/[^\p{L}\p{N}\s]/u', '', $string);

解决办法是这样的: $string = preg_replace('/[^\p{L}\p{N}\s]/u', '', $string);

回答by Arpit Gaur

$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.

$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.

echo $string; // output will be abcdef-g for 'a|"bc!@£de^&$f g'

回答by Vijaya Pandey

Try this:

尝试这个:

<?php

$string = "Remove these characters: £?ó";
$string = preg_replace('/[^(\x20-\x7F)]*/','', $string);
echo $string;
?>

回答by mark

As per the other answers, build a regular expression for the characters you permit (eg. from each alphabet you support, and remove other characters. Here's a list of Unicode Block Rangesto get the character values/ranges for each language - that's something you'll need to compile yourself.

根据其他答案,为您允许的字符构建一个正则表达式(例如,从您支持的每个字母表中删除其他字符。这是一个Unicode 块范围列表,用于获取每种语言的字符值/范围 - 这就是您需要自己编译。

回答by RMcLeod

It is possible to check for unicode characters and numbers, but only if PCRE was compiled with
--enable-unicode-properties. If this is the case then you can use regex \p{Nl}which will match unicode letters and numbers. A lot more information on unicode regex in PHP can be found in the documentation

可以检查 unicode 字符和数字,但前提是 PCRE 是用
--enable-unicode-properties. 如果是这种情况,那么您可以使用正则表达式\p{Nl}来匹配 unicode 字母和数字。有关 PHP 中 unicode regex 的更多信息可以在文档中找到

EDIT:After edit to question

编辑:编辑后提出问题

To get symbols use \Pinstead of \pagain look at the PHP documentation I linked to above.

要使用符号\P而不是\p再次查看我上面链接的 PHP 文档。