我有数千个字符串,我只想从中选择那些没有任何特殊字符的字符串。特殊字符包括 ;:~?[]()+-_*^%$#@><{}'|/
和数字 0-9。所以基本上有效的句子是那些包含字母或带逗号的字母的句子。
最快的方法,以便可以快速有效地完成任务。
例:
1. She has the air of blank disdainful amusement a cat gets when toying with a mouse
2. Origin Expand 1535-1545 1535-45; disdain + -ful Related forms Expand disdainfully, adverb disdainfulness, noun Synonyms Expand contemptuous, haughty, contumelious
3. British Dictionary definitions for disdainful Expand disdainful /dsdenfl/ adjective
4.An example of someone who is disdainful, is a person saying they dislike someone just because of their religion
应选择第1句和第4句
所以我需要类似的东西
if( $s does not have number an does not have special character)
{
//Save it
}
任何帮助将不胜感激艾哈迈尔
if (! preg_match('/[''0-9^£$%&*()}{@#~?><>|=_+¬-]/', $string))
{
// No special characters or numbers found in this string.
}
^[^;:~?'[']()+_*^%$#@><{}'|'/0-9-]+$
您也可以尝试此操作。请参阅演示。
http://regex101.com/r/oE6jJ1/32
$re = "/^[^;:~?''['']()+_*^%$#@><{}''|''/0-9-]+$/im";
$str = "She has the air of blank disdainful amusement a cat gets when toying with a mouse 'n'nOrigin Expand 1535-1545 1535-45; disdain + -ful Related forms Expand disdainfully, adverb disdainfulness, noun Synonyms Expand contemptuous, haughty, contumelious'n'nBritish Dictionary definitions for disdainful Expand disdainful /dsdenfl/ adjective'n'nAn example of someone who is disdainful, is a person saying they dislike someone just because of their religion";
preg_match_all($re, $str, $matches);
构建自己的正则表达式以仅传递字母、空格、逗号。
^[a-zA-Z's,]+$
或
^[a-zA-Z'h,]+$
或
^['p{L}'h,]+$
演示
'h
匹配水平空间。因此,^[a-zA-Z'h,]+$
匹配具有一个或多个空格、字母或逗号的行。 'p{L}
可以匹配任何语言的任何类型的字母。
if ( preg_match('~^[a-zA-Z'h,]+$~m', $string))
{
// do here
}
最有效的方法是检测字符串是否至少包含一个您不需要的字符:
使用范围:假设您只处理 ASCII 字符
if ( !preg_match('/[!-+--@[-`{-~]/', $str )) {
// the string is allowed
}
或用于更广泛的用途:使用 POSIX 字符类
if ( !preg_match('/[^[:alpha:],[:space:]]/', $str )) {
// the string is allowed
}
//your string variable:
$string = "She has the air of blank disdainful amusement a cat gets when toying with a mouse
Origin Expand 1535-1545 1535-45; disdain + -ful Related forms Expand disdainfully, adverb disdainfulness, noun Synonyms Expand contemptuous, haughty, contumelious
British Dictionary definitions for disdainful Expand disdainful /dsdenfl/ adjective
An example of someone who is disdainful, is a person saying they dislike someone just because of their religion";
//match function, and echo output
preg_match_all('/^[a-z, ]+$/gmi', $string, $matches);
foreach($matches[0] as $found){
echo $found . "'n"; //echoes sentences 1 and 4
}