- DOMDocument::getElementsByTagName
- Parameters
- Return Values
- Examples
- See Also
- User Contributed Notes 14 notes
- Get all HTML tags from string using PHP?
- Answer
- Solution:
- Share solution ↓
- Additional Information:
- Didn’t find the answer?
- Similar questions
- Write quick answer
- About the technologies asked in this question
- PHP
- HTML
- Welcome to programmierfrage.com
- Get answers to specific questions
- Help Others Solve Their Issues
DOMDocument::getElementsByTagName
This function returns a new instance of class DOMNodeList containing all the elements with a given local tag name.
Parameters
The local name (without namespace) of the tag to match on. The special value * matches all tags.
Return Values
A new DOMNodeList object containing all the matched elements.
Examples
Example #1 Basic Usage Example
$dom = new DOMDocument ;
$dom -> loadXML ( $xml );
$books = $dom -> getElementsByTagName ( ‘book’ );
foreach ( $books as $book ) echo $book -> nodeValue , PHP_EOL ;
>
?>
The above example will output:
Patterns of Enterprise Application Architecture Design Patterns: Elements of Reusable Software Design Clean Code
See Also
User Contributed Notes 14 notes
Return if there are no matches is an empty DOMNodeList. Check using length property, e.g.:
$nodes = $domDocument -> getElementsByTagName ( ‘book’ ) ;
if ( $nodes -> length == 0 ) <
// no results
>
?>
Note that when using getElementsByTagName that it is a dynamic list. Thus if you have code which adjusts the DOM structure it will change the results of the getElementsByTagName results list.
The following code iterates through a complete set of results and changes them all to a new tag:
$nodes = $xml -> getElementsByTagName ( «oldtag» );
$nodeListLength = $nodes -> length ; // this value will also change
for ( $i = 0 ; $i < $nodeListLength ; $i ++)
$node = $nodes -> item ( 0 );
// some code to change the tag name from «oldtag» to something else
// e.g. encrypting a tag element
>
?>
Since the list is dynamically updating, $nodes->item(0) is the next «unchanged» tag.
My first post!
And this is how I get elements by attribute and its value.
For an example, if I want to grab all DIV tags with class name ‘className’, then.
$some_link = ‘some website’ ;
$tagName = ‘div’ ;
$attrName = ‘class’ ;
$attrValue = ‘className’ ;
$dom = new DOMDocument ;
$dom -> preserveWhiteSpace = false ;
@ $dom -> loadHTMLFile ( $some_link );
$html = getTags ( $dom , $tagName , $attrName , $attrValue );
echo $html ;
function getTags ( $dom , $tagName , $attrName , $attrValue ) $html = » ;
$domxpath = new DOMXPath ( $dom );
$newDom = new DOMDocument ;
$newDom -> formatOutput = true ;
$filtered = $domxpath -> query ( «// $tagName » . ‘[@’ . $attrName . «=’ $attrValue ‘]» );
// $filtered = $domxpath->query(‘//div[@class=»className»]’);
// ‘//’ when you don’t know ‘absolute’ path
// since above returns DomNodeList Object
// I use following routine to convert it to string(html); copied it from someone’s post in this site. Thank you.
$i = 0 ;
while( $myItem = $filtered -> item ( $i ++) ) $node = $newDom -> importNode ( $myItem , true ); // import node
$newDom -> appendChild ( $node ); // append node
>
$html = $newDom -> saveHTML ();
return $html ;
>
?>
Please, improve it, and share it.
Following Example is of multiple attributes and multiple child nodes. this is being used to make joomla plugin for bulk upload of articles. Gurmukh Singh Bhatti
$dom = new DomDocument ;
$dom -> preserveWhiteSpace = FALSE ;
$dom -> loadXML ( $xml );
$params = $dom -> getElementsByTagName ( ‘section’ ); // Find Sections
$k = 0 ;
foreach ( $params as $param ) //go to each section 1 by 1
echo «Section Attribute :-> » . $params -> item ( $k )-> getAttribute ( ‘name’ ). «
» ; //get section attribute
$params2 = $params -> item ( $k )-> getElementsByTagName ( ‘category’ ); //digg categories with in Section
$i = 0 ; // values is used to iterate categories
foreach ( $params2 as $p ) echo » — Category Attribute Name :-> » . $params2 -> item ( $i )-> getAttribute ( ‘name’ ). «
» ; //get Category attributes
$params3 = $params2 -> item ( $i )-> getElementsByTagName ( ‘arti’ ); //dig Arti into Categories
$j = 0 ; //values used to interate Arti
foreach ( $params3 as $p2 )
echo » — Article Attribute Name : » . $params3 -> item ( $j )-> getAttribute ( ‘name’ ). «» ; //get arti atributes
echo » Value : » . $params3 -> item ( $j )-> nodeValue . «
» ; //get Node value ;
$j ++;
>
$i ++;
>
$k ++;
>
?>
output :
Section Attribute :-> Section1
— Category Attribute Name :-> google
— Article Attribute Name : article1 Value : any html code heremy name is so so
— Article Attribute Name : article2 Value : value2
— Article Attribute Name : article3 Value : value3
— Article Attribute Name : article4 Value : value4
— Category Attribute Name :-> yahoo
— Article Attribute Name : articleSection2 Value : Test value
Section Attribute :-> Section2
— Category Attribute Name :-> msn
— Article Attribute Name : article2 Value : value1
— Article Attribute Name : article3 Value : value2
— Category Attribute Name :-> webcare
— Article Attribute Name : param3 Value : value4
Here is an example of getElementsByTagName():
$dom = new DomDocument ;
$dom -> preserveWhiteSpace = FALSE ;
$dom -> loadXML ( $xml );
$params = $dom -> getElementsByTagName ( ‘param’ );
foreach ( $params as $param ) echo $param -> getAttribute ( ‘name’ ). ‘
‘ ;
>
?>
Expected result:
—————
param1
param2
param3
This is a very simplistic way to traverse xml nodes and childnodes using the DOMDocument class
I was in need of $dom->getElementsByTagName that would hist magic within a contextNode.
Why i needed getElementsByTagName instead of just simple using an xPath->query is because while looping through the returned nodelist, more nodes with the tagName i was looking for where created.
When using getElementsByTagName, the new nodes are «added» to the nodelist i already am looping through.
When using an xpath query, you wil only loop through the original nodelist, the newly created elements wil not appear in that nodelist.
I was already using an extended class on domDocument, so it was simple to create an kind of getElementsByTagName that would accept an contextNode.
class SmartDocument extends DOMDocument private $localDom ;
public $xpath ;
private $serialize = array( ‘localDom’ );
private $elemName ;
private $elemCounter ;
/**
* Constructor
*/
function __construct () parent :: __construct ( ‘1.0’ , ‘UTF-8’ );
$this -> preserveWhiteSpace = false ;
$this -> recover = TRUE ;
$this -> xpath = new DOMXpath ( $this );
>
/**
* GetElementsByTagname within an contextNode
*
* @param string $name
* @param DomNode $contextNode
* @return DOMNode|NULL
*/
public function getElementsByTagNameContext ( $name , $contextNode )
if( $this -> elemName != $name ) $this -> elemCounter = 0 ;
$this -> elemName = $name ;
>
$this -> elemLength = $this -> xpath -> evaluate ( ‘count(.//*[name() keyword»>. $this -> elemName . ‘»])’ , $contextNode );
while( $this -> elemCounter < $this ->elemLength ) $this -> elemCounter ++;
$nl = $this -> xpath -> query ( ‘.//*[name() keyword»>. $this -> elemName . ‘»][‘ . $this -> elemCounter . ‘]’ , $contextNode );
if( $nl -> length == 1 ) return $nl -> item ( 0 );
>
>
$this -> elemLength = null ;
$this -> elemCounter = null ;
$this -> elemName = null ;
return null ;
>
>
$doc = new SmartDocument ();
$doc -> load ( ‘book.xml’ );
$nl = $doc -> query ( ‘//books’ );
foreach( $nl as $node ) while( $book = $doc -> getElementsByTagNameContext ( ‘book’ , $node )) //When you now create new nodes within this loop as child or following-sibling of this node
// They show up within this loop
>
Get all HTML tags from string using PHP?
For example, we have this string:
"some textsome textText
"
I nedd to get all tags in array like this:
and if there is some tags like i will just replace all strange tags with valid ones:
Answer
Solution:
Try using DOMDocument. See the documentation here. You can search about DOMDocument example in Stack Overflow.
Share solution ↓
Additional Information:
Didn’t find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
PHP (from the English Hypertext Preprocessor — hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites. The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML
HTML (English «hyper text markup language» — hypertext markup language) is a special markup language that is used to create sites on the Internet. Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.