【问题标题】:I need to extract a part of the url in an iframe我需要在 iframe 中提取部分 url
【发布时间】:2017-12-19 20:18:32
【问题描述】:
<iframe src="https://player.vimeo.com/video/322332324?byline=0" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>

我需要从 src 中提取数字。我需要获得 322332324。不管有多少位。

此代码将获取 src,但我如何仅获取数字

preg_match('/src="([^"]+)"/', $iframe_string, $match); $url = $match[1];

【问题讨论】:

  • 帮助是否有任何不会改变的已知信息。有吗?你能列出来吗?
  • 您可以使用parse_url() 获取路径,在/ 上分解它并获取最后一个元素。
  • 这总是一样的 "src="player.vimeo.com/video"
  • 既然您删除了我的回答的评论说它有帮助(以及您没有接受它的事实),这是否意味着它对您不起作用?如果没有,请告诉我会发生什么。
  • 非常感谢您的帮助,成功了!!

标签: php iframe


【解决方案1】:

假设数字始终是 URL 的最后一部分,您可以这样做:

备选方案 1

$url = 'https://player.vimeo.com/video/322332324?byline=0';

// Extract the path from the URL
$path = parse_url($url, PHP_URL_PATH);

// Explode the path on / to get the different fragments
$fragments = explode('/', $path);

// Now simply take the last part of the fragemnts
$numbers = end($fragments);

演示:https://3v4l.org/TI1El

备选方案 2

如果你想要更少的代码,你可以跳过分解部分,只需要 substr() 和 strrpos()。更少的代码但更少的冗长:

$url = 'https://player.vimeo.com/video/322332324?byline=0';

// Get the path from the URL
$path = parse_url($url, PHP_URL_PATH);

// Get the everything after the last slash
$numbers = substr($path, strrpos($path, '/') + 1);

演示:https://3v4l.org/aRjfc

【讨论】:

  • @IsmailMathkour - 如果它解决了您的问题,请随时接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多