PHP parses HTML tags

Possible duplicate:
How to parse and process HTML using PHP?

I am new to PHP. I have body tag text on some page in a string variable. I would like to know if it contains some tag ... where the tag1 tag name is indicated, and if so, take only that tag from the string. How can I do this simply in PHP?

Thanks!!

+6
source share
3 answers

You would look at something like this:

<?php $content = ""; $doc = new DOMDocument(); $doc->load("example.html"); $items = $doc->getElementsByTagName('tag1'); if(count($items) > 0) //Only if tag1 items are found { foreach ($items as $tag1) { // Do something with $tag1->nodeValue and save your modifications $content .= $tag1->nodeValue; } } else { $content = $doc->saveHTML(); } echo $content; ?> 

DomDocument is an entire HTML or XML document; serves as the root of the document tree. This way you will have valid markup, and if you find the items by tag name, you will not find comments.

+11
source

Another possibility is regular expression.

 $matches = null; $returnValue = preg_match_all('#<li.*?>(.*?)</li>#', 'abc', $matches); 

$matches[0][x] contains all matches, such as <li class="small">list entry</li> , $matches[1][x] contains only internal HTML, for example list entry .

+2
source

Quick way:

Look at the index position tag1, then find the index position / tag 1. Then cut the line between the two indexes. Take a look at strpos and substr on php.net. Also, this may not work if your line is too long.

 $pos1 = strpos($bigString, '<tag1>'); $pos2 = strpos($bigString, '</tag1>'); $resultingString = substr($bigString, -$pos1, $pos2); 

You may need to add and / or subtract some units from $ pos1 and $ pos2 to get the correct value of $ resultString. (if you have no comments with tag1 inside them, sigh)

The right way:

Search for html parsers

0
source

All Articles