How to match a part of an <iframe> tag?

自闭症网瘾萝莉.ら 提交于 2019-12-20 03:00:10

问题


I'm trying to match the highlighted parts of this string:

<iframe maybe something here src="http://some.random.url.com/" and the string continues...

I need to match the src="" if it's placed inside of an tag. The iframe tag can be placed anywhere in the source.

Thanks in advance! :)


回答1:


You should use a DOM parser for that. Here's an example with DOMDocument :

<?php
    $document = new DOMDocument();
    $document->loadHTML(file_get_contents('yourFileNameHere.html'));
    $lst = $document->getElementsByTagName('iframe');

    for ($i=0; $i<$lst->length; $i++) {
        $iframe= $lst->item($i);
        echo $iframe->attributes->getNamedItem('src')->value, '<br />';
    }
?>



回答2:


<?php
$html='<iframe maybe somethin gere src="http://some.random.url.com/" and blablabla';

preg_match('|<iframe [^>]*(src="[^"]+")[^>]*|', $html, $matches);

var_dump($matches);

Output:

array(2) {
  [0]=>
  string(75) "<iframe maybe somethin gere src="http://some.random.url.com/" and blablabla"
  [1]=>
  string(33) "src="http://some.random.url.com/""
}

But this is a quick way to do this using regular expression, which may break with unclean html or cause problems, go for a dom parser for a good proof solution.




回答3:


If youre source is well formed xml you can also use xpath to find the string.

<?php
  $file = simplexml_load_file("file.html");
  $result = $file->xpath("//iframe[@src]/@src");
?>



回答4:


A regular expression is going to be the cleanest way to do it:

preg_match('<iframe.+?src="(.+?)".+?<\/iframe>', $iframe);

print_r($iframe);

array([0] => whole reg ex match, [1] => your src url);



回答5:


see RegEx match open tags except XHTML self-contained tags

That said, your particular situation isn't really parsing... just string matching. Methods for that have already been enumerated before my answer here...




回答6:


You should use a DOM parser, but this regex would get you started if there is a reason you must use regexes

.*(?<iframeOpening><iframe)\s[^>]*(?<iframeSrc>src=['"][^>'"]+['"]?).*

It uses named capture groups by the way, here's how they work

preg_match('/.*(?<iframeOpening><iframe)\s[^>]*src=[\'"](?<iframeSrc>[^>\'"])+[\'"]?.*/', $searchText, $groups);
print_r($groups['iframeSrc']);


来源:https://stackoverflow.com/questions/3636365/how-to-match-a-part-of-an-iframe-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!