Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP数组-如何保存几行以匹配一长串字符_Php_Arrays - Fatal编程技术网

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

我在用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 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
}