Home  >  Q&A  >  body text

请教个关于php正则表达式的问题。。。

有点特殊需求,需要一条表达式取出html中N个字段数据
比如

<p class="test">
    <p class="title">xxx</p>
    <p class="xas"></p><pclass="asd"></p>
    <p class="pic">xxx</p>
</p>
<p class="test">
    <p class="title">xxx</p>
    <p class="xas"></p><pclass="asd"></p>
    <p class="pic">xxx</p>
</p>

需要在html中取出title以及pic的数据 正则该怎么写呢?
中间的 <p class="xas"></p><pclass="asd"></p> 为不同的内容 但是这些内容没什么用 没必要抓取
刚入门 请教一下 谢谢

迷茫迷茫2737 days ago437

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 13:11:53

    You don’t have to use regular expressions, there is something called phpquery

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-16 13:11:53

    let str = `
        <p class="test">
            <p class="title">xxx</p>
            <p class="xas"></p><pclass="asd"></p>
            <p class="pic">xxx</p>
        </p>
        <p class="test">
            <p class="title">xxx</p>
            <p class="xas"></p><pclass="asd"></p>
            <p class="pic">xxx</p>
        </p>
    `
    
    let results = str.match(/(title|pic)">.*?</g).map(e=>e.replace('">', ':')).map(e=>e.replace('<', ''))

    The result is:

    // results:
    [ 'title:xxx', 'pic:xxx', 'title:xxx', 'pic:xxx' ]

    Supplement:
    I didn’t see clearly that the question mentioned PHP. I don’t know much about PHP. It’s written in js. You can just take the regular part.

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-16 13:11:53

    Just upload the code

    $re = '/(title|pic).*?>([^<]+)/';
    $str = '<p class="test">
        <p class="title">xxx</p>
        <p class="xas"></p><pclass="asd"></p>
        <p class="pic">xxx</p>
    </p>
    <p class="test">
        <p class="title">xxx</p>
        <p class="xas"></p><pclass="asd"></p>
        <p class="pic">xxx</p>
    </p>';
    
    preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
    
    //演示输出的结果,你可以根据你的需求,从$matches中拿到你想要的xxx部分。
    var_dump($matches);

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-16 13:11:53

    This kind of performance is very poor -.-. It is still reliable to use js to obtain it at the front desk.

    reply
    0
  • Cancelreply