Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.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 - Fatal编程技术网

如何在php中匹配字符串模式

如何在php中匹配字符串模式,php,Php,我有一个字符串数组 $x=array("Hello","How","You","iam","fine") 我试图用这样的模式提取一个字符串 $y=preg_grep ("/Hello(\w+)/", $x); print_r($y); 我想用一个preg\u grep进行多模式搜索并返回。有人能帮我吗。Regex quantifier+表示匹配1次或多次。字符串Hello不合适,因为Hello后面有0个符号。使用*量词,表示匹配零次或多次: $x=array("Hello","How",

我有一个字符串数组

$x=array("Hello","How","You","iam","fine") 
我试图用这样的模式提取一个字符串

$y=preg_grep ("/Hello(\w+)/", $x);

print_r($y);

我想用一个
preg\u grep
进行多模式搜索并返回。有人能帮我吗。

Regex quantifier
+
表示
匹配1次或多次
。字符串
Hello
不合适,因为
Hello
后面有0个符号。使用
*
量词,表示
匹配零次或多次

$x=array("Hello","How","You","iam","fine");
$y=preg_grep ("/Hello(\w*)/", $x);
print_r($y);
// outputs: Array ( [0] => Hello )

正则表达式量词
+
表示
匹配1次或多次
。字符串
Hello
不合适,因为
Hello
后面有0个符号。使用
*
量词,表示
匹配零次或多次

$x=array("Hello","How","You","iam","fine");
$y=preg_grep ("/Hello(\w*)/", $x);
print_r($y);
// outputs: Array ( [0] => Hello )

您可以尝试以下方法:

$y = preg_grep("/(PATTERN_1|PATTERN_2)/", $x);

我可以问一下为什么不想在模式上使用循环吗?

您可以尝试以下方法:

$y = preg_grep("/(PATTERN_1|PATTERN_2)/", $x);
我可以问你为什么不想在图案上使用循环吗?

你可以试试这个

<?php    
    $x=array("Hello","How","You","iam","fine");
    $y=preg_grep ("/H(\w*)|Y(\w*)/", $x);
    print_r($y);
?>

您可以试试这个

<?php    
    $x=array("Hello","How","You","iam","fine");
    $y=preg_grep ("/H(\w*)|Y(\w*)/", $x);
    print_r($y);
?>


在问题中回显您的输出在问题中准确回显您的输出。它正在搜索“Hello[word]”,但数组中没有这样的东西。他们可能还想在组之前添加一个空格,除非他们正在搜索Helloweenth。这并不能回答问题,是吗?完全正确。它正在搜索“Hello[word]”,但数组中没有这样的东西。他们可能还想在组前添加一个空格,除非他们正在搜索Helloweenth。这并不能回答这个问题,对吗?“我想使用一个preg_grep进行多模式搜索并返回,任何人都可以帮助我。”我的回答完全符合作者的条件:一个preg_grep,多模式搜索并返回。“我想用一个preg_grep进行多模式搜索并返回,任何人都可以帮助我。”我的回答完全符合作者的条件:一个preg_grep,多模式搜索并返回。