PHP正前pression返回子匹配的数组 [英] PHP Regular expression return submatches as array

查看:175
本文介绍了PHP正前pression返回子匹配的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于定期EX pressions的问题。

我想要做的是只用一个常规的前pression匹配字符串的一部分,并得到了什么内部的分歧。不知道怎么解释,所以会写一个例子

示例HTML解析

 < D​​IV CLASS =测试>
    &所述;跨度>一种与所述; /跨度>
    <跨度> B< / SPAN>
    &所述;跨度>℃下/跨度>
    &所述;跨度D 1和D&下; /跨度>
< / DIV>
< D​​IV CLASS =test2的>
    <跨度> AA< / SPAN>
    <跨度> BB< / SPAN>
    &所述;跨度> cc的下; /跨度>
    <跨度> DD< / SPAN>
< / DIV>

我要preg_match(_all)只能从.TEST跨度值

通常情况下,我会用

  preg_match('/< D​​IV CLASS =测试方​​式>(*)< \\ / DIV> /',$ HTML,$匹配)
preg_match_all('/&下;跨度>(*)&所述;?\\ /跨度&GT /',$比赛[1],$ matches2)

然后用另一个preg_match_all走出值。

不过,我想知道是否有一种方法,使一个子模式中,将全自动第一场比赛的div,然后所有的跨度和将返回resulat为阵的格局。

是这样的可能吗?我到处都找不到它。也许我不知道它是怎么技术上称为

编辑:
输出我想获得(更改的数据样本),但只与一个preg_match或preg_match_all通话

 阵列(
    '一个',
    'B',
    'C',
    'D',
);


解决方案

使用的DOMParser ,而不是去正规的前pressions ..

  $ DOM =新的DOM文档;
$ dom-> loadHTML($ HTML);
的foreach($ dom->的getElementsByTagName('DIV')为$标签){
    如果($标签也即>的getAttribute(类)===测试)
    {
        的foreach($标签也即>的getElementsByTagName('跨')为$雄鹿)
        {
        $ VAL [] = $ stag->的nodeValue;
        }
    }
}
的print_r($ VAL);

使用XPath查询..(同一)

  $ = XPath的新DOMXpath($ DOM);
$元素= $ xpath->查询(* / DIV [@类='测试'] / SPAN);
的foreach($元素作为$ V)
{
    $常用3 [] = $ V->的nodeValue;
}
的print_r($ ARR);

输出:

 阵列

    [0] =>一个
    [1] => b
    [2] => C
    [3] => ð

<大骨节病> 工作演示 - 普通DOM路

<大骨节病> 工作演示 - XPath的方式

I have a question regarding regular expressions.

What I want to do is use only one regular expression to match part of string and get out what's inside divided. Don't know how to explain it, so will write an example

Example html to parse

<div class="test">
    <span>a</span>
    <span>b</span>
    <span>c</span>
    <span>d</span>
</div>
<div class="test2">
    <span>aa</span>
    <span>bb</span>
    <span>cc</span>
    <span>dd</span>
</div>

I want to preg_match(_all) only span values from .test

Normally, I would use

preg_match('/<div class="test">(.*?)<\/div>/', $html, $matches)
preg_match_all('/<span>(.*?)<\/span>/',  $matches[1],  $matches2)

And then use another preg_match_all to get out values.

However, i was wondering if there is a way to make a subpattern in a pattern that would automaticly first match divs and then all spans and would return resulat as array.

Is something like this possible? I couldn't find it anywhere. Maybe I don't know how it is technically called.

Edit: Output I would like to get (changed data sample), but only with one preg_match or preg_match_all call:

array(
    'a',
    'b',
    'c',
    'd',
);

解决方案

Use a DOMParser instead of going for regular expressions..

$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('div') as $tag) {
    if ($tag->getAttribute('class') === 'test')
    {
        foreach($tag->getElementsByTagName('span') as $stag)
        {
        $val[]=$stag->nodeValue;
        }
    }
}
print_r($val);

Using an XPath Query.. (for the same)

$xpath = new DOMXpath($dom);
$elements = $xpath->query("*/div[@class='test']/span");
foreach($elements as $v)
{
    $arr[]=$v->nodeValue;
}
print_r($arr);

OUTPUT :

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)

Working Demo - Normal DOM Way

Working Demo - XPath Way

这篇关于PHP正前pression返回子匹配的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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