简体   繁体   English

如何使用PHP Regex / preg_match提取以下信息?

[英]How do I extract the following information using PHP Regex / preg_match?

I have the following HTML and I need to extract the URL inside VALUE 我有以下HTML,我需要提取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? 我究竟做错了什么?

If you really want to use preg_match you can do: 如果您确实想使用preg_match ,则可以执行以下操作:

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. 问题在于没有在标签末尾转义/符号,并且您可能没有理由将"'彼此相邻。

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. 注意修饰符U表示Ungreedy ,它允许.*紧凑并且不会返回超出我们实际需要的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM