【问题标题】:Removing all Href and youtube URL and geting the ID删除所有 Href 和 youtube URL 并获取 ID
【发布时间】:2014-01-30 06:49:15
【问题描述】:
$article = ('<p>In show business it is all about the extremes. For example the people
 below earn a whole load of money thanks to their appearance and it is not at all 
beautiful. They are some of the ugliest models but nevertheless they still work in 
full swing. They get hired in movies, commercials, and anywhere where an ugly guy 
(or a girl) is needed , and if you’re just plane average and you still wish to 
work, well then you should probably do something extreme with your body like the 
lizard man, for example.</p></a>
<a href="http://www.youtube.com/watch?v=eauCiY1MmZU">
    http://www.youtube.com/watch?v=eauCiY1MmZU
</a>

function tubeCodeEmbed( $vCode ) {
   return $vCode;
}

$search = '/\s*[a-zA-Z\/\/:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i';
$article = preg_replace($search, tubeCodeEmbed("$1"), $article);
echo $article;

这是我到目前为止所拥有的......它可以工作,只是它不会从包含 youtube 的字符串中删除 href。

我想要的输出

在演艺界,一切都是关于极端的。例如人们 由于他们的外表,下面赚了一大笔钱 一点都不漂亮。 joeCiY1MmZU 他们是最丑的模特 但尽管如此,他们仍然如火如荼地工作。他们被录用 电影、广告以及任何有丑男(或女孩)的地方 需要,如果你只是平庸而你仍然想工作, 那么你可能应该对你的身体做一些极端的事情,比如 例如蜥蜴人。奇数CiY1MmZU eauCiY1MmZU


注意输出中的 ID ...没有来自 youtube 的 url 或 href 标签

【问题讨论】:

  • 我试过了……它不能满足我的需要。
  • @JosephPhilbert,你能用你问的output 编辑你的问题吗?
  • 我需要删除 youtube 链接/主机的“HREF”标签。
  • 试试这个 '$html = preg_replace_callback("/(?([^/", '', $html);函数 parseID($matches){ if (preg_match("%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?: mbed)?)/|.*[?&]v=)|youtu\.be/)([^\"&?/ ]{11})%i", $matches[0], $match)) { $video_id = $match[1]; // 这里是视频 id } } echo $final;'

标签: php regex youtube


【解决方案1】:
$article=explode("?v=",$article);
$article=explode("\"",$article[1]);
$youtube_id=$article[0];

$youtube_id 将返回视频的 id

【讨论】:

    【解决方案2】:

    看看这段代码能不能用。

    工作PHP Fiddle

    <?php
    $article = '<p>In show business it is all about the extremes. For example the people
     below earn a whole load of money thanks to their appearance and it is not at all 
    beautiful. They are some of the ugliest models but nevertheless they still work in 
    full swing. They get hired in movies, commercials, and anywhere where an ugly guy 
    (or a girl) is needed , and if you’re just plane average and you still wish to 
    work, well then you should probably do something extreme with your body like the 
    lizard man, for example.</p><a href="http://www.youtube.com/watch?v=eauCiY1MmZU">http://www.youtube.com/watch?v=eauCiY1MmZU</a>';
    
    
    function get_id($matches){
      parse_str( parse_url( $matches[2], PHP_URL_QUERY ), $tArr );
      return " ".$tArr["v"];
    }
    $string = preg_replace("/<p[^>]*?>/", "",$article);
    $string = str_replace("</p>", "", $string);
    $string = preg_replace_callback("#<a(.+?)>(.+?)</a>#is", "get_id", $string);
    
    echo $string;
    
    ?>
    

    输出:

    在演艺界,一切都是关于极端的。例如人们 由于他们的外表,下面赚了一大笔钱 一点都不漂亮。它们是一些最丑陋的模型,但 尽管如此,他们仍然如火如荼地工作。他们被电影​​聘用, 广告,以及任何需要丑男(或女孩)的地方, 如果你只是平庸而你仍然想工作,那么 你可能应该对你的身体做一些极端的事情,比如 以蜥蜴人为例。 eauCiY1MmZU eauCiY1MmZU

    【讨论】:

    • 为什么会有两次ID? “eauCiY1MmZU eauCiY1MmZU”也不应该删除其他href链接
    • 我是按照你说的你想要的输出做的。在您的输出更新中,您列出了ID twice。看到更新,我改了。你的标题也写了所有的href。
    • 好的,我想通了 这会删除所有带有 youtube 链接的 href $article-&gt;text = preg_replace('#&lt;a href="https?://www.youtube.*?&gt;([^&gt;]*)&lt;/a&gt;#i', '$1', $article-&gt;text); 然后得到 ID $ychar = '/\s*[a-zA-Z\/\/:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&amp;\;\%\=\.]*)/i'; $1 是 ID (PERFECT) $article-&gt;text = preg_replace($ychar, $this-&gt;youtubeCodeEmbed("$1"),$article-&gt;text);
    【解决方案3】:
    //Strip all youtube links from the $article.
        if ((preg_match('/http:\/\/www\.youtube\.com\/watch\?v=[^&]+/',$article->text))==true) {
        $article->text = preg_replace('/(<a [^>]+)>(http:\/\/*.*.youtube.*)  <\/a>|iU/iU', '$2', $article->text);
        $ychar = '/\s*[a-zA-Z\/\/:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i';
        $article->text = preg_replace($ychar, $this->youtubeCodeEmbed("$1"), $article->text);
        return true;
    //Strip all vimeo links from the $article.
    } elseif ((preg_match('/http:\/\/(.*?)vimeo\.com\/[0-9]+/', $article->text))==true) {
        $article->text = preg_replace('/(<a [^>]+)>(http:\/\/*.*.vimeo.*)<\/a>|iU/iU', '$2', $article->text);
        $vchar = '/\s*[a-zA-Z\/\/:\.]*vimeo.com\/([0-9\-_]+)([0-9\/\*\-\_\?\&\;\%\=\.]*)/i';
        $article->text = preg_replace($vchar, $this->vimeoCodeEmbed('$1'), $article->text);
        return true;
        } else {
    return false;
    }
    

    }

    经过一些挖掘和探索,这是一个可行的解决方案,并且效果很好。 首先 preg_replace 删除给定域的所有 href URL,然后...删除其他 preg_replace 中的其余部分

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 2011-10-17
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多