PHP数组-如何保存几行以匹配一长串字符
我在用PHP做数组时遇到了一个问题。 我不知道如何为以下情况编写代码:PHP数组-如何保存几行以匹配一长串字符,php,arrays,Php,Arrays,我在用PHP做数组时遇到了一个问题。 我不知道如何为以下情况编写代码: $arrs = array("a@c", "cr", "exd", "hello", "gg%j", "hwa", "@", "8"); foreach ($arrs as $arr){ // if $arr does not belong to any characters from a to z, // then there must be some special character in it. // Say f
$arrs = array("a@c", "cr", "exd", "hello", "gg%j", "hwa", "@", "8");
foreach ($arrs as $arr){
// if $arr does not belong to any characters from a to z,
// then there must be some special character in it.
// Say for example, "a@c" will still be regarded as fine string,
// since it contains normal characters a and c.
// another example, "gg%j" will also be fine, since it contains g and j.
}
您可以使用正则表达式和函数:
$arrs = array("abc", "cr", "exd", "hello", "gj", "hwa", "@", "8");
foreach ($arrs as $arr){
// if $arr does not belong to any characters from a to z,
// then there must be some special character in it.
if (!preg_match('/^[a-z]*$/', $arr)) {
var_dump($arr);
}
}
这将为您提供以下输出:
string '@' (length=1)
string '8' (length=1)
这里,正则表达式匹配:
- 字符串开头:
^
和a
之间的任意字符数:z
[a-z]*
- 字符串结尾:
$
因此,如果字符串包含不在
a
和z
之间的任何内容,它将不匹配;和var\u dump
字符串。您可以使用正则表达式,函数:
$arrs = array("abc", "cr", "exd", "hello", "gj", "hwa", "@", "8");
foreach ($arrs as $arr){
// if $arr does not belong to any characters from a to z,
// then there must be some special character in it.
if (!preg_match('/^[a-z]*$/', $arr)) {
var_dump($arr);
}
}
$arrs = array("abc", "cr", "exd", "hello", "gj", "hwa", "@", "8");
foreach ($arrs as $arr){
// if $arr does not belong to any characters from a to z,
// then there must be some special character in it.
if(preg_match("/[a-zA-Z]+/",$arr)==0)
{
echo "There must be a special character in it.";
}
}
这将为您提供以下输出:
string '@' (length=1)
string '8' (length=1)
这里,正则表达式匹配:
- 字符串开头:
^
和a
之间的任意字符数:z
[a-z]*
- 字符串结尾:
$
a
和z
之间的任何内容,它将不匹配;和var\u dump
字符串
$arrs = array("abc", "cr", "exd", "hello", "gj", "hwa", "@", "8");
foreach ($arrs as $arr){
// if $arr does not belong to any characters from a to z,
// then there must be some special character in it.
if(preg_match("/[a-zA-Z]+/",$arr)==0)
{
echo "There must be a special character in it.";
}
}
这听起来像是一个可疑的家庭作业问题
if(!preg_match("/^[a-z]*$/", $arr)) {
// $arr has something other than just a to z in it
}
这听起来像是一个可疑的家庭作业问题
if(!preg_match("/^[a-z]*$/", $arr)) {
// $arr has something other than just a to z in it
}