开发者

Can't match any of this subject with preg_match

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开发者_如何学C 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜