在php中提取部分网址

时间:2016-10-12 21:52:28

标签: php

我有一个网址:http://domain.tld/123456789abc.html

我的目标是创建一个这样的嵌入代码:

<iframe src="http://domain.tld/embed-123456789abc-620x360.html" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="620" height="360"></iframe>

但不是给我这个:

<iframe src="http://domain.tld/embed-123456789abc.html-620x360.html" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="620" height="360"></iframe>

注意上面包含的“.html”(123456789abc.html-620x360)?如何在没有源URL的“.html”的情况下创建代码?

这是正在使用的代码。

elseif (substr_count($link,"domain")){
        $video_id = explode("/",$link);
        if (isset($video_id[count($video_id)-1])){
            $video_id = $video_id[count($video_id)-1];
            $embed = '<IFRAME SRC="http://domain.tld/embed-'.$video_id.'-'.$width.'x'.$height.'.html" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="'.$width.'" height="'.$height.'"></iframe>';
        }

感谢帮助。谢谢。

2 个答案:

答案 0 :(得分:0)

你需要'清理'$ video_id,因为“.html”应该包含在你要检索的数组组件中。

所以而不是

$video_id = $video_id[count($video_id)-1];

尝试这样的事情:

$video_id = str_replace('.html', '', $video_id[count($video_id)-1]);

答案 1 :(得分:0)

使用正则表达式来匹配你的最后一部分可能更容易 原始链接,已排除.html

elseif (substr_count($link,"domain")) {
    $embed = preg_replace(
        '#.+/([^/]+).html$#',
        '<iframe src="http://domain.tld/embed-$1-'
            . $width . 'x' . $height . '.html"'
            . 'frameborder="0" marginwidth="0" marginheight="0" scrolling="no"'
            . 'width="' . $width . '" height="' . $height . '"></iframe>',
        $link
    );
}