0

I'm trying to use regular expressions in PHP to filter a large string ($content) and save all iFrame code (from begining to end <iframe ... </iframe) to an array. I have tried the following with no success.

preg_match_all('#<iframe([^>]+?)src=[\'"]?([^\'"\s>]+)[\'"]?([^>]*)>#', $content, $matches, PREG_OFFSET_CAPTURE);
$video1 = $matches[1][0];
var_dump($video1);

With this I am only able to return one video. I am no expert on regex but feel this should be relatively simple to do.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
9ete
  • 3,692
  • 1
  • 34
  • 32
  • 2
    `[^>]*` is greedy, `[^>]*?` is not. But have you thought about using something like SimpleHTMLDom instead? – kero Jan 22 '15 at 21:01
  • 1
    http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – TecBrat Jan 22 '15 at 21:07
  • 2
    **Don't use regular expressions to parse HTML. Use a proper HTML parsing module.** You cannot reliably parse HTML with regular expressions, and you will face sorrow and frustration down the road. As soon as the HTML changes from your expectations, your code will be broken. See my http://htmlparsing.com/php or [this SO thread](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) for examples of how to properly parse HTML with PHP modules that have already been written, tested and debugged. – Andy Lester Jan 22 '15 at 21:10
  • Thank you very much for the help everyone. I have created a new question about solving this problem with html parsing. I've gotten close but no cigar, yet. I think one of you will have the solution. Thanks again. http://stackoverflow.com/questions/28118437/parse-html-with-htmldom-replace-all-iframe-tags-with-another – 9ete Jan 23 '15 at 20:46

0 Answers0