Php 在iframe标记内选择URL的一部分

Php 在iframe标记内选择URL的一部分,php,regex,iframe,preg-match,Php,Regex,Iframe,Preg Match,我需要捕获一个ID,该ID是iframe标记中URL的一部分。 我知道这可以用正则表达式完成,但我不是很擅长,我做了一些尝试,但没有得到结果,如果框架是这样的(ID可能会有所不同): 此正则表达式与源属性匹配,并将您指定的id放入组1中。 src=“.+\/(.+?)” 第一部分src=“匹配属性的开头 +\/匹配URL正文,直到最后一个斜杠(贪婪) (.+?)”匹配您的id(惰性)和关闭属性的双引号 您可以使用正则表达式拆分字符串 $re = '/\<iframe[^\>]+sr

我需要捕获一个ID,该ID是iframe标记中URL的一部分。 我知道这可以用正则表达式完成,但我不是很擅长,我做了一些尝试,但没有得到结果,如果框架是这样的(ID可能会有所不同):


此正则表达式与源属性匹配,并将您指定的id放入组1中。
src=“.+\/(.+?)”

  • 第一部分
    src=“
    匹配属性的开头
  • +\/
    匹配URL正文,直到最后一个斜杠(贪婪)
  • (.+?)”
    匹配您的id(惰性)和关闭属性的双引号

您可以使用正则表达式拆分字符串

$re = '/\<iframe[^\>]+src\="(.+?)\/([A-Za-z0-9]+)"/';
$str = '<iframe src="https://www.example.com/embed/ph57d6z9fa1349b" frameborder="0" height="481" width="608" scrolling="no"></iframe>';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);
$re='/\]+src\=“(.+?)\/([A-Za-z0-9]+)”/”;
$str='';
预匹配全部($re,$str,$matches,预设置顺序,0);
//打印整个匹配结果
var_dump($matches);
如果要列出id代码(如“ph57d6z9fa1349b”),则可以执行以下操作:

<?php
$re = '/\<iframe[^\>]+src\="(.+?)\/([A-Za-z0-9]+)"/';
$str = '<iframe src="https://www.example.com/embed/ph57d6z9fa1349b" frameborder="0" height="481" width="608" scrolling="no"></iframe>';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

foreach ($matches as $match) {
    $id = $match[2];    // The required id code
    echo $id;           // Echo it
}
?>

这是不是太过分了。除息的
$re = '/\<iframe[^\>]+src\="(.+?)\/([A-Za-z0-9]+)"/';
$str = '<iframe src="https://www.example.com/embed/ph57d6z9fa1349b" frameborder="0" height="481" width="608" scrolling="no"></iframe>';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);
<?php
$re = '/\<iframe[^\>]+src\="(.+?)\/([A-Za-z0-9]+)"/';
$str = '<iframe src="https://www.example.com/embed/ph57d6z9fa1349b" frameborder="0" height="481" width="608" scrolling="no"></iframe>';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

foreach ($matches as $match) {
    $id = $match[2];    // The required id code
    echo $id;           // Echo it
}
?>