views:

14

answers:

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? Or perhaps someone could perpose a better way of doing what I want.

Thanks.

+1  A: 

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
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