2012-01-12 40 views
-1

如果我有一个数组:如何在数组中使用preg匹配?

Array 
(
    [0] => Array 
     (
      [0] => |174|September|2001| 
      [1] => |Pengantar=Hello!!!! 
      [2] => |Tema= Sami Mawon 
      [3] => |Tema_isi=meet you!!! 
      [4] => |Kutip=people 
      [5] => |Kutip_kitab=Efesus 
      [6] => |Kutip_pasal=4 
      [7] => |Kutip_ayat=28 
      [8] => |Tema_sumber=Kiriman dari Maurits albert ([email protected]) 
      [9] => [[Kategori:e-humor 2001]] 
     ) 

我怎样才能得到Pengantar,泰马,Tema_isi等方面的价值?

+0

请说明您想要的值以及匹配这些值的条件。 – salathe 2012-01-12 09:16:36

回答

1

您将不得不遍历数组,并使用带引用的preg_match。 正则表达式东西像这样(把我的头顶部)可能会工作:

/\|(.*?)=(.*?)|?/ 

只需使用preg_match('/\|(.*?)=(.*?)|?/', $subject[$x], $matches);var_dump($matches);看到的结果。

不要忘记,传递给preg_match函数的$ matches数组是对数组的引用,您应该首先实例化它,并在每个循环中覆盖它。

+0

但我无法获得像** hello !!!,sami mawon,见到你!!!,人**等等的感谢之前的价值。 对不起,我是初学者.. – 2012-01-13 02:09:18

1

只使用数组的步行路程,匹配REG EX和lambda函数:

$array = array(
    array(
     '|174|September|2001|', 
     'Pengantar=Hello!!!!', 
     'Tema= Sami Mawon ', 
     'Kategori:e-humor 2001', 
      [...] 
    ) 
); 

$values = array(); 

array_walk($array[0],function(&$item1, $key) use(&$values) { 
    if(preg_match('#[^=]=(.+)#',$item1,$match)){ 
     $values[] = $match[1]; 
    } 
}); 

print_r($values); 
0
从问题

除了“哪个模式”,我建议你看看到preg_replace­Docs这是能够在阵列操作直。

$pattern = "..."; 
$matches = preg_replace($pattern, '\1', $array); 
0

从问题中不清楚,但它看起来像你想preg_filter

该函数可以执行一个正则表达式匹配并替换一个数组,返回一个只包含匹配项的替换值的新数组。

1

正则表达式,可就是这样,用"preg_match()"named subpattern: -

$a = array(
    array(
     '|174|September|2001|', 
     '|Pengantar=Hello!!!!', 
     '|Tema= Sami Mawon', 
     '|Tema_isi=meet you!!!', 
     '|Kutip=people', 
     '|Kutip_kitab=Efesus', 
     '|Kutip_pasal=4', 
     '|Kutip_ayat=28', 
     '|Tema_sumber=Kiriman dari Maurits albert ([email protected])', 
     '[[Kategori:e-humor 2001]]', 
    ) 
); 
$pattern = '/(?<first>\w+)[:=](?<rest>[\d|\w|\s]+)/'; 
$matches = array(); 
foreach ($a as $_arrEach) { 
    foreach ($_arrEach as $_each) { 
     $result = preg_match($pattern, $_each, $matches[]); 
    } 
} 

echo "<pre>"; 
print_r($matches); 
echo "</pre>"; 

你会发现,在数组键“first”满足您的要求。

上面会输出: -

Array 
(
    [0] => Array 
     (
     ) 

    [1] => Array 
     (
      [0] => Pengantar=Hello 
      [first] => Pengantar 
      [1] => Pengantar 
      [rest] => Hello 
      [2] => Hello 
     ) 

    [2] => Array 
     (
      [0] => Tema= Sami Mawon 
      [first] => Tema 
      [1] => Tema 
      [rest] => Sami Mawon 
      [2] => Sami Mawon 
     ) 

    [3] => Array 
     (
      [0] => Tema_isi=meet you 
      [first] => Tema_isi 
      [1] => Tema_isi 
      [rest] => meet you 
      [2] => meet you 
     ) 

    [4] => Array 
     (
      [0] => Kutip=people 
      [first] => Kutip 
      [1] => Kutip 
      [rest] => people 
      [2] => people 
     ) 

    [5] => Array 
     (
      [0] => Kutip_kitab=Efesus 
      [first] => Kutip_kitab 
      [1] => Kutip_kitab 
      [rest] => Efesus 
      [2] => Efesus 
     ) 

    [6] => Array 
     (
      [0] => Kutip_pasal=4 
      [first] => Kutip_pasal 
      [1] => Kutip_pasal 
      [rest] => 4 
      [2] => 4 
     ) 

    [7] => Array 
     (
      [0] => Kutip_ayat=28 
      [first] => Kutip_ayat 
      [1] => Kutip_ayat 
      [rest] => 28 
      [2] => 28 
     ) 

    [8] => Array 
     (
      [0] => Tema_sumber=Kiriman dari Maurits albert 
      [first] => Tema_sumber 
      [1] => Tema_sumber 
      [rest] => Kiriman dari Maurits albert 
      [2] => Kiriman dari Maurits albert 
     ) 

    [9] => Array 
     (
      [0] => Kategori:e 
      [first] => Kategori 
      [1] => Kategori 
      [rest] => e 
      [2] => e 
     ) 
) 

希望它能帮助。