开发者

How to use php regular get the 2ed and 3rd `<div class="partright">`?

开发者 https://www.devze.com 2023-02-08 04:23 出处:网络
There have some texts, how to use php regular get the 2ed and 3rd <div class=\"partright\">? Thanks.

There have some texts, how to use php regular get the 2ed and 3rd <div class="partright">? Thanks.

<div class="wrap">
  <div class="content>
    <div class="partleft">
    text1
    </div>
    <div class="partright">
    text2
    </div>
  </div>
  <div class="content>
    <div class="partleft">
    text3
    </div>
    <div class="partright">开发者_JS百科
    text4
    </div>
  </div>
  <div class="content>
    <div class="partleft">
    text5
    </div>
    <div class="partright">
    text6
    </div>
  </div>
</div>

I want output

<div class="partright">
text4
</div>
<div class="partright">
text6
</div>


Your question is very incomplete but I assume your talking about traversing the elements to modify them in some way.

You should look at the following library called SimpleDOM

And usage would be like:

require_once 'simple_dom.class.php';

$html = "<html_data_here>";
$html = str_get_html($html);

foreach($html->find(".partleft:nth(2),.partleft:nth(3)") as $p)
{
    echo $p->outerText;
}

Note: The above is an example and may not work as expected, for working examples please see the Simple Dom site linked above.


You can't parse [X]HTML with regex. RegEx match open tags except XHTML self-contained tags use SimpleXML indeed.


You could build a regular expression that would match

<div class="partright">(.*)</div>

Put all matches in an array and take the 2nd and third element from the array.

0

精彩评论

暂无评论...
验证码 换一张
取 消