如何在preg_match数组搜索? [英] How to search in an array with preg_match?

查看:157
本文介绍了如何在preg_match数组搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在preg_match一个数组搜索?

例如:

 < PHP
如果(preg_match('/(我的\\ N +字符串\\ N +)/ I',阵列('文件','我的字符串=>名','这个'),$比赛))
{
    //外观极好!
    $项目[] = $匹配[1];
}其他{
    //UPS!未找到!
}
?>


解决方案

在这篇文章中,我会为你提供做你要求什么的三种不同的方法。其实,我建议使用的最后一个片段,因为它是最简单的COM prehend以及作为在code相当整齐。

如何看什么元素,符合我的正常前pression一个数组?

有一个专门用于这个目的的功能, preg_grep 。这将需要一个普通的前pression作为第一个参数,一个数组作为第二。

请参阅下面的例子:

  $大海捞针=阵列(
  '问好',
  你好计算器,
  '你好,世界',
  '富酒吧浅
);$匹配= preg_grep('/ ^你好(\\ w +)/ I',$草堆);的print_r($比赛);

输出

 阵列

    [1] =>你好计算器
    [2] =>你好,世界

文档


,但我只是想获得指定组的值。怎么样?

array_reduce preg_match 可以解决干净利索这一问题;看到下面的代码片段。

  $大海捞针=阵列(
  '问好',
  你好计算器,
  '你好,世界',
  '富酒吧浅
);功能_matcher($ M,$ STR){
  如果(preg_match('/ ^你好(\\ w +)/ I',$海峡,$匹配))
    $ M [] = $匹配[1];  返回$米;
}// 注意 :
// ------------------------------------------------ ------------------------------
//你可以直接指定_matcher作为一个匿名函数
// array_reduce虽然那种降低了可读性,因此
//不推荐,但它是可能的。$匹配= array_reduce($草堆,_matcher,阵列());的print_r($比赛);

输出

 阵列

    [0] =>堆栈溢出
    [1] =>世界

文档


使用 array_reduce 似乎有些单调乏味,是不是有另一种方式?

是的,这一次居然吸尘器虽然它不涉及使用任何pre-现有的阵列_ * $ P $皮克_ * 功能。

包装在一个函数,如果你要使用这种方法一次以上。

  $匹配=阵列();的foreach($大海捞针为$ STR)
  如果(preg_match('/ ^你好(\\ w +)/ I',$海峡,$ M))
    $匹配[] = $ M [1];

文档

How do I search in an array with 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!
}
?>

解决方案

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.

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.

$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?

Yes, and this one is actually cleaner though it doesn't involve using any pre-existing array_* or preg_* function.

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

这篇关于如何在preg_match数组搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆