Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Element is iFrame or Object (PHP-Xpath)

Tags:

dom

php

xpath

I wrote a script that uses PHP DOM. I'm parsing a webpage and accessing the elements through XPath.

Everything works fine except when it comes to getting a Node that will have either an tag or an tag.

What I want to do is basically:

if element tag is 'object' then < execute code here >

elseif element tag is 'iFrame' then < excecute alternative code>

Some Div Blocks will have: Code:

<div class="scenario1" > 
<object src=xxxx... /> 

While others will have:

Code:

<div class="scenario2" > 
<iFrame src=xxxx... />

This is the the only part left that's messing me up now. Any idea how do differentiate between an element type?It always displays a blank line if its an IFrame (I have an Xpath leading to the parent node that check for the object tag). I've tried checking if the node is empty/null then trying to search for a iFrame but it wasn't working for me and returned a blank line.

like image 537
user2464665 Avatar asked Jan 29 '26 05:01

user2464665


1 Answers

Use the tagName property, for example if $elem is a DOMElement:

if(strtolower($elem->tagName) == 'iframe'))
{
    // it's an iframe
}
like image 168
MrCode Avatar answered Jan 31 '26 20:01

MrCode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!