php 如何使用 preg_match 在数组中搜索?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8627334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to search in an array with preg_match?
提问by Olaf
How do I search in an array with preg_match?
如何使用 preg_match 在数组中搜索?
Example:
例子:
<?php
if( preg_match( '/(my\n+string\n+)/i' , array( 'file' , 'my string => name', 'this') , $match) )
{
//Excelent!!
$items[] = $match[1];
} else {
//Ups! not found!
}
?>
回答by Filip Roséen - refp
In this post I'll provide you with three different methods of doing what you ask for. I actually recommend using the last snippet, since it's easiest to comprehend as well as being quite neat in code.
在这篇文章中,我将为您提供三种不同的方法来完成您的要求。我实际上建议使用最后一个片段,因为它最容易理解并且代码非常简洁。
How do I see what elements in an array that matches my regular expression?
如何查看数组中与我的正则表达式匹配的元素?
There is a function dedicated for just this purpose, preg_grep
. It will take a regular expression as first parameter, and an array as the second.
有一个专门用于此目的的功能,preg_grep
。它将使用正则表达式作为第一个参数,将数组作为第二个参数。
See the below example:
请参阅以下示例:
$haystack = array (
'say hello',
'hello stackoverflow',
'hello world',
'foo bar bas'
);
$matches = preg_grep ('/^hello (\w+)/i', $haystack);
print_r ($matches);
output
输出
Array
(
[1] => hello stackoverflow
[2] => hello world
)
Documentation
文档
But I just want to get the value of the specified groups. How?
但我只想获取指定组的值。如何?
array_reduce
with preg_match
can solve this issue in clean manner; see the snippet below.
array_reduce
withpreg_match
可以干净利落地解决这个问题;请参阅下面的片段。
$haystack = array (
'say hello',
'hello stackoverflow',
'hello world',
'foo bar bas'
);
function _matcher ($m, $str) {
if (preg_match ('/^hello (\w+)/i', $str, $matches))
$m[] = $matches[1];
return $m;
}
// N O T E :
// ------------------------------------------------------------------------------
// you could specify '_matcher' as an anonymous function directly to
// array_reduce though that kind of decreases readability and is therefore
// not recommended, but it is possible.
$matches = array_reduce ($haystack, '_matcher', array ());
print_r ($matches);
output
输出
Array
(
[0] => stackoverflow
[1] => world
)
Documentation
文档
Using array_reduce
seems tedious, isn't there another way?
使用array_reduce
似乎很乏味,难道没有其他方法吗?
Yes, and this one is actually cleaner though it doesn't involve using any pre-existing array_*
or preg_*
function.
是的,虽然它不涉及使用任何预先存在的array_*
或preg_*
功能,但它实际上更干净。
Wrap it in a function if you are going to use this method more than once.
如果您要多次使用此方法,请将其包装在一个函数中。
$matches = array ();
foreach ($haystack as $str)
if (preg_match ('/^hello (\w+)/i', $str, $m))
$matches[] = $m[1];
Documentation
文档
回答by Galen
回答by Logan Serman
You can use array_walk
to apply your preg_match
function to each element of the array.
您可以使用array_walk
将您的preg_match
函数应用于数组的每个元素。
回答by goat
$items = array();
foreach ($haystacks as $haystack) {
if (preg_match($pattern, $haystack, $matches)
$items[] = $matches[1];
}
回答by Bashirpour
$haystack = array (
'say hello',
'hello stackoverflow',
'hello world',
'foo bar bas'
);
$matches = preg_grep('/hello/i', $haystack);
print_r($matches);
Output
输出
Array
(
[1] => say hello
[2] => hello stackoverflow
[3] => hello world
)