如何在数组中使用Preg Match? [英] How to use preg match in array?

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

问题描述

如果我有一个数组:

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 (romind@ )
            [9] => [[Kategori:e-humor 2001]]
        ) 

如何获取Pengantar,Tema,Tema_isi等的值?

How can I get the value of Pengantar, Tema, Tema_isi etc?

推荐答案

正则表达式可以像这样,使用命名子模式 http://php.net/manual/zh-CN/function.preg-match.php rel = nofollow> preg_m atch() :-

The regular expression can be like this, using the named subpattern of "preg_match()":-

$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 (romind@ )',
        '[[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 满足您的要求。

You will find that the array key "first" satisfies your requirement.

上面的内容将输出为:-

The above will output as:-

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
        )
)

希望有帮助。

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

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