1

I am trying to match some of the Javascript on a Youtube video page. The pattern thats the same for every video is like this:

<param name=\"flashvars\" value=\"somethingsomethingsomething\">

I need to get out everything in value, in this case somethingsomethingsomething. It is commented out because this is embedded in Javascript. This is the code I'm using:

preg_match('<param name=\\"flashvars\\" value=\\"(.*)\\">', $ytPage, $match);

$ytPage is the source code of the youtube page. But when I run the code $matches never returns a match.

Is there something I'm doing wrong?

durron597
  • 31,968
  • 17
  • 99
  • 158
Will
  • 10,731
  • 6
  • 22
  • 16
  • 1
    possible duplicate of [RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – durron597 Sep 07 '15 at 21:24
  • possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – Artjom B. Sep 07 '15 at 21:33

1 Answers1

1

Your problem is that you should surround your regular expression with delimiters, for example slashes.

preg_match('/...../', ...);

Example code:

$ytPage= "<param name=\"flashvars\" value=\"somethingsomethingsomething\">";
preg_match('/<param name=\\"flashvars\\" value=\\"(.*)\\">/', $ytPage, $match);
print_r($match)

Result:

Array
(
    [0] => <param name="flashvars" value="somethingsomethingsomething">
    [1] => somethingsomethingsomething
)

ideone

If you are trying to parse HTML you might want to consider if an HTML parser would be a more suitable tool than regular expressions.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • Sorry, `$matches` was a typo. The real variable is called `$match`. The fixes you mentioned above also don't work. But I think I've found the problem. Only part of the Youtube videos source code is being returned in $ytPage. I don't know whether this is a restriction from cURL (the way I'm getting the page) or if it's something else... but now at least I know this part works. Thanks for your help :) – Will Sep 16 '10 at 23:59