删除所有Href和youtube网址并获取ID

时间:2014-01-30 06:49:15

标签: php regex youtube

$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的字符串中去除hrefs。

我想要的输出

  

在演艺界,这完全是极端的事情。例如人民   由于它们的外观,它可以获得一大笔钱   一点也不漂亮。 joeCiY1MmZU他们是一些最丑陋的模特   但尽管如此,他们仍然全力以赴。他们被录用了   电影,广告,以及丑陋的男人(或女孩)所在的任何地方   需要,如果你只是飞机平均值,你仍然希望工作,   那么你应该做一些极端的事情,就像你的身体一样   例如,蜥蜴人。 oddCiY1MmZU eauCiY1MmZU


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

3 个答案:

答案 0 :(得分:0)

$article=explode("?v=",$article);
$article=explode("\"",$article[1]);
$youtube_id=$article[0];

$ youtube_id将返回视频ID

答案 1 :(得分:0)

查看此代码是否有效。

正在使用 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

答案 2 :(得分:0)

//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中的其余部分