开发者

PHP get tag url

Hello If i want this text:

$content = '<div id="hey">
   <div id="bla"></div>
</div>

<div id="hey">
 hey lol
</div>';
  • The content inside the id="hey" can b开发者_StackOverflow中文版e changed.

    And now I want to get the tags in array

    $array[0] = < div id="bla"></div >;
    $array[1] = < hey lol >;
    

How Can I do that? i though about preg_match_all?


Sounds to me, if I understand this correctly, you're looking to parse HTML with PHP. Though regex can work, it's certainly not the best method.

With that said, have a look at the DOMDocument class. It allows you to parse HTML files, and has methods similar to javascript in terms of referencing elements by tag, id, etc.

Per your example:

<?php

$html = '<div id="hey">hey lol</div>'; /* or file_get_contents('...'); */

$dom = new DOMDocument();
$dom->loadHTML($html);

// this will get <div id="hey"></div>
$hey_div = $dom->getElementById('hey');

echo $hey_div->textContent; // "hey lol"


$content=str_replace("hey","bla",$content);

OR

$divid="hey";
//$divid="bla";
$content = '<div id="' . $divid . '">
   <div id="bla"></div>
</div>

<div id="hey">
 hey lol
</div>';
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜