I'd like to remove all numbers from a string [0-9]. I wrote this code that is working:
我想从字符串[0-9]中删除所有数字。我写的这段代码是有效的:
$words = preg_replace('/0/', '', $words ); // remove numbers
$words = preg_replace('/1/', '', $words ); // remove numbers
$words = preg_replace('/2/', '', $words ); // remove numbers
$words = preg_replace('/3/', '', $words ); // remove numbers
$words = preg_replace('/4/', '', $words ); // remove numbers
$words = preg_replace('/5/', '', $words ); // remove numbers
$words = preg_replace('/6/', '', $words ); // remove numbers
$words = preg_replace('/7/', '', $words ); // remove numbers
$words = preg_replace('/8/', '', $words ); // remove numbers
$words = preg_replace('/9/', '', $words ); // remove numbers
I'd like to find a more elegant solution: 1 line code (IMO write nice code is important).
我想找到一个更优雅的解决方案:一行代码(我觉得写漂亮的代码很重要)。
The other code I found in * also remove the Diacritics (á,ñ,ž...).
*中的其他代码我也删除附加符号(n,ž……)。
5 个解决方案
#1
103
For Western Arabic numbers (0-9):
西方阿拉伯数字(0-9):
$words = preg_replace('/[0-9]+/', '', $words);
For all numerals including Western Arabic (e.g. Indian):
所有数字,包括西阿拉伯语(例如印度语):
$words = '१३३७';
$words = preg_replace('/\d+/u', '', $words);
var_dump($words); // string(0) ""
-
\d+
matches multiple numerals. - \ d +匹配多个数字。
- The modifier
/u
enables unicode string treatment. This modifier is important, otherwise the numerals would not match. - 修改器/u支持unicode字符串处理。这个修饰语很重要,否则数字不匹配。
#2
41
Try with regex \d
:
试着用正则表达式\ d:
$words = preg_replace('/\d/', '', $words );
\d
is an equivalent for [0-9]
which is an equivalent for numbers range from 0
to 9
.
\d对于[0-9]是等价的,对于从0到9的数字是等价的。
#3
5
Use some regex like [0-9]
or \d
:
使用一些regex,如[0-9]或\d:
$words = preg_replace('/\d+/', '', $words );
You might want to read the preg_replace() documentation as this is directly shown there.
您可能想要阅读preg_replace()文档,因为它直接显示在那里。
#4
0
Regex
正则表达式
$words = preg_replace('#[0-9 ]*#', '', $words);
#5
0
Use Predefined Character Ranges
使用预定义的字符范围
echo $words= preg_replace('/[[:digit:]]/','', $words);
echo $的话= preg_replace(' /[[数位:]]/”,“美元的话);
#1
103
For Western Arabic numbers (0-9):
西方阿拉伯数字(0-9):
$words = preg_replace('/[0-9]+/', '', $words);
For all numerals including Western Arabic (e.g. Indian):
所有数字,包括西阿拉伯语(例如印度语):
$words = '१३३७';
$words = preg_replace('/\d+/u', '', $words);
var_dump($words); // string(0) ""
-
\d+
matches multiple numerals. - \ d +匹配多个数字。
- The modifier
/u
enables unicode string treatment. This modifier is important, otherwise the numerals would not match. - 修改器/u支持unicode字符串处理。这个修饰语很重要,否则数字不匹配。
#2
41
Try with regex \d
:
试着用正则表达式\ d:
$words = preg_replace('/\d/', '', $words );
\d
is an equivalent for [0-9]
which is an equivalent for numbers range from 0
to 9
.
\d对于[0-9]是等价的,对于从0到9的数字是等价的。
#3
5
Use some regex like [0-9]
or \d
:
使用一些regex,如[0-9]或\d:
$words = preg_replace('/\d+/', '', $words );
You might want to read the preg_replace() documentation as this is directly shown there.
您可能想要阅读preg_replace()文档,因为它直接显示在那里。
#4
0
Regex
正则表达式
$words = preg_replace('#[0-9 ]*#', '', $words);
#5
0
Use Predefined Character Ranges
使用预定义的字符范围
echo $words= preg_replace('/[[:digit:]]/','', $words);
echo $的话= preg_replace(' /[[数位:]]/”,“美元的话);