0

I tried to get the src of an iframe inside of an html using this

preg_match('/src=\'([^\']+)\'/', $fresult, $match);

but this code sometimes fails.

some sugest that I use an DomDocument But I can't found a reg exp sample

$doc = new DOMDocument();
$doc->loadHTML($html); 
foreach ($tags as $tag) {
    echo $tag->nodeValue;
}

How do I get the src value of frame?

sample

<iframe src='test.com' />

i should have test.com

also how do I do the preg_match_all equivalent of DomDocument?

like this

<html>
<label class="su">test1</label>
<label class="su">test2</label>
<label class="su">test3</label>
</html>

which is I should have a array result for test1, test2 and test3

I am new to this dom php thing. so please don't be harsh. thanks

  • You're **NOT** supposed to use regular expressions in this case. People suggested the DomDocument because it's designed to parse HTML. Regular expressions cannot handler the nested nature of the language properly. Do not use regular expressions with DomDocument either. The `DomDocument` solution is supposed to be entirely independent of Regex. – d0nut Oct 20 '15 at 15:28
  • Use `DomDocument::getElementsByTagName` and search for `"iframe"`. after that, get the `src` attribute from the elements returned. – d0nut Oct 20 '15 at 15:30

1 Answers1

3

First of all, welcome to Stack Overflow! Please do not use regular expressions on DOM documents (see here why). Instead, please stick to PHP DomDocument.

That said, you may get an iframe tag and src like so:

$doc = new DOMDocument();
$doc->loadHTML(...); 
$frame= $doc->getElementsByTagName('iframe')->item(0);
$src = $frame->getAttribute('src');

Concerning your second question, you might want to have a look at DOMXPath:

$doc = new DOMDocument();
$doc->loadHTMLFile(...);

$xp = new DOMXPath($dom);
$labels = $xp->query('//label[@class="su"]');
Community
  • 1
  • 1
Jan
  • 42,290
  • 8
  • 54
  • 79