How to get all tags from a HTML code inside a string?
Example:
$HTML = <<<HTML
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<p id="main">Hello World!</p>
<img src="wallpaper.png">
</body>
</html>
HTML;
getTags($HTML);
Print out (return Array) - Tags:
array(html, head, meta, title, body, p, img)
or
Print out - Tags with id attribute:
array(html, head, meta, title, body, [p, main], img)
Just FYI, I am new to HTML.
This does everything but print out the ids, but it shouldn't be too hard to figure out:
<?php
$html = '<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<p id="main">Hello World!</p>
<img src="wallpaper.png">
</body>
</html>';
$dom = new DOMDocument();
$list=array();
$dom->loadHTML($html);
$elements = $dom->getElementsByTagName('*');
foreach($elements as $child)
{
$list[]= $child->nodeName;
}
?>
Answer:
Array ( [0] => html [1] => head [2] => meta [3] => title [4] => body [5] => p [6] => img )
Use a HTML parser to read the string. A quick Google indicates you can do this with PHP directly: http://php.net/manual/en/domdocument.loadhtml.php
I'm sure there are other decent HTML parsers available, too.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.