从字符串中获取字符串?爆炸

  • 本文关键字:字符串 爆炸 获取 php
  • 更新时间 :
  • 英文 :


我有以下字符串

<embed src='herp.com'  width='240' height='180'  allowscriptaccess='always' allowfullscreen='true' flashvars='volume=94&stretching=fill&file=http%3A%2F%2Fmedia.cdn.com%2FTHEMP%2Fflash%2Ffile.mp4&plugins=viral-1d'/>

我想从中得到http%3A%2F%2Fmedia.cdn.com%2FTHEMP%2Fflash%2Ffile.mp4&plugins=viral-1d

我在想通过 = 爆炸然后获取倒数第二个值,但这可能容易出错(例如,如果他们在 flashvars 变量之后添加另一个herp="blah",脚本将不再工作),有没有其他方法可以更防弹我需要的字符串周围的语法更改?

$str = "<embed src='herp.com'  width='240' height='180'  allowscriptaccess='always' allowfullscreen='true' flashvars='volume=94&stretching=fill&file=http%3A%2F%2Fmedia.cdn.com%2FTHEMP%2Fflash%2Ffile.mp4&plugins=viral-1d'/>";
// figure out where the params begin (keep the starting quote)
$strpos = strpos($str, "flashvars=") + strlen("flashvars=");
$str = substr($str, $strpos);
// get the quoting char
$delimiter = $str[0];
// first match strtok returns is our param list
$str = strtok($str, $delimiter);
parse_str($str, $params);
var_dump($params);

这里的正确方法是使用适当的 HTML 解析库解析 HTML,并从 <embed> 标记中提取 flashvars 属性。但是,如果您只有其中之一,则实际上可以使用正则表达式。

表达式将检索 flashvars 属性,并将该值传递给parse_str()以检索所有查询字符串组件。 parse_str()会打电话给他们urldecode(),所以你不需要。

// Regex gets the entire flahsvars
$pattern = "/<embed[^>]+flashvars='([^']+)'/";
preg_match($pattern, $embed, $matches);
// $matches[1] now holds the full contents of `flashvars`
// Then parse_str() on the result:
$parts = array();
parse_str($matches[1], $parts);
print_r($parts);
// The part you want is in the file key:
echo $parts['file'];

Array
(
    [volume] => 94
    [stretching] => fill
    [file] => http://media.cdn.com/THEMP/flash/file.mp4
    [plugins] => viral-1d
)

所用正则表达式的说明:

/<embed[^>]+flashvars='([^']+)'/

它首先查找<embed,然后查找除结束>[^>]+)之外的任何字符。 flashvars= 后面的捕获组将查找 flashvars 属性上结束引号之前但不包括结束引号的所有字符,并将它们存储在第一个捕获组 $matches[1] 中。

有一个更好的方法,看看:

http://php.net/manual/en/function.parse-str.php

它解析 URL 的查询字符串。当然,如果您必须先删除所有多余的内容。只需使用正则表达式提取查询字符串

最新更新