亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

I have a question about php regular expressions. . .
迷茫
迷茫 2017-05-16 13:09:53
0
4
521

There are some special needs. An expression is needed to extract N field data in html
For example

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

We need to extract the title and pic data from HTML. How to write the regular expression?
The <p class="xas"></p><pclass="asd"></p> in the middle is different content, but this content is useless and there is no need to crawl it
I'm just getting started. Please give me some advice. Thank you

迷茫
迷茫

業(yè)精于勤,荒于嬉;行成于思,毀于隨。

reply all(4)
Ty80

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

我想大聲告訴你
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.

Peter_Zhu

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

//演示輸出的結(jié)果,你可以根據(jù)你的需求,從$matches中拿到你想要的xxx部分。
var_dump($matches);
淡淡煙草味

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template