0

I have the following HTML and I need to extract the URL inside VALUE

<param name="movie" value="http://domain.com/path/to/file.swf" />

I tried the following, with no success.

preg_match("'<param name=\"movie\" value=\"(.*?)\" />si'", $source,  $url); 
echo $url[1];

What am I doing wrong?

user1985705
  • 43
  • 1
  • 4
  • 5
    You shouldn't use regex to parse HTML, use [DOM Document](http://php.net/manual/en/class.domdocument.php) – Jay Blanchard Jan 21 '15 at 21:36
  • Guess what will happen if your markup code starts looking like this ``. If you need a regex to solve a markup problem then you have at least two problems. –  Jan 21 '15 at 21:45
  • I am clueless, what will happen? Is this piece of code not properly coded? I am confused. – user1985705 Jan 21 '15 at 22:00
  • More information on why not to use a regex for this [here](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – DarkBee Jan 21 '15 at 22:45

2 Answers2

1

If you really want to use preg_match you can do:

preg_match('/<param name=\"movie\" value=\"(.*?)\" \/>/is', $source, $url);
echo $url[1]

Problem was with not escaping a / symbol at the end of a tag and you have " and ' next to each other without a reason probably.

speccode
  • 1,562
  • 9
  • 11
  • You can use other delimiters than `/`. And the original code attempted to use `'` single quotes, which would be fine, if the regex `is` flags had been moved **after** *↓ :)* the ending `'` delimiter. – mario Jan 21 '15 at 21:45
  • 1
    @mario Maybe, but it would be better if they were **after** the ending delimiter. ;) – Anonymous Jan 21 '15 at 21:47
0

My, correct variant:

$source = '<param name="movie" value="http://domain.com/path/to/file.swf" />';

preg_match('!<param name="movie" value="(.*?)"!U', $source,  $url); 
echo $url[1];

The result:

http://domain.com/path/to/file.swf

Note the modifier U which means Ungreedy, it allows .* to be compact and not return more than we actually need.

Oleg Dubas
  • 2,320
  • 1
  • 10
  • 24