Php 删除所有Href和youtube URL并获取ID

Php 删除所有Href和youtube URL并获取ID,php,regex,youtube,Php,Regex,Youtube,这就是我到目前为止所做的。它可以工作,只是它不会从包含youtube的字符串中删除HREF 我的期望输出 在演艺界,一切都是极端的。比如人民 由于他们的外表,下面的人赚了一大笔钱 一点也不漂亮。他们是一些最丑的模特 但尽管如此,它们仍在如火如荼地工作。他们被雇用了 电影,商业广告,以及任何丑陋的男人或女孩所在的地方 需要,如果你只是普通人,你还想工作, 那么你应该对你的身体做一些极端的事情,比如 例如,蜥蜴人。奇怪的 注意输出中的ID…没有来自youtube的url或href标记 $articl

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

我的期望输出 在演艺界,一切都是极端的。比如人民 由于他们的外表,下面的人赚了一大笔钱 一点也不漂亮。他们是一些最丑的模特 但尽管如此,它们仍在如火如荼地工作。他们被雇用了 电影,商业广告,以及任何丑陋的男人或女孩所在的地方 需要,如果你只是普通人,你还想工作, 那么你应该对你的身体做一些极端的事情,比如 例如,蜥蜴人。奇怪的

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

$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\u id将返回视频的id

查看此代码是否有效

工作

输出:

在演艺界,一切都是极端的。比如人民 由于他们的外表,下面的人赚了一大笔钱 一点也不漂亮。他们是一些最丑的模特,但是 尽管如此,他们仍在全力工作。他们受雇于电影业, 无论是商业广告还是任何需要丑男丑女的地方, 如果你只是一个普通人,你仍然想工作,那么 你可能应该对你的身体做一些极端的事情,比如 例如,蜥蜴人。eauciymzu eauciymzu

}

经过一些挖掘和刺激,这是一个有效的解决方案,效果非常好。
首先preg_replace删除给定域的所有href URL,然后。。。删除另一个preg_replace中的其余部分

我尝试过的可能重复项。。。它不能满足我的需要。@JosephPhilbert,你能用你所问的输出编辑你的问题吗?我需要删除youtube链接/主机的HREF标记。试试这个“$html=preg\u replace\u callback/?为什么它有两次ID?eauCiY1MmZU eauCiY1MmZU也不应该删除其他href链接。我是根据您所说的想要输出的方式来做的。在输出更新中,您列出了两次ID。看到更新了吗,我改了。你的标题上还显示了所有的href。好吧,我想这会删除所有带有youtube链接的href$article->text=preg_替换为'I'、'$1'、$article->text;然后得到ID$ychar='/\s*[a-zA-Z\/\:\.]*youtube.com\/watch\?v=[a-zA-Z0-9\-\\+[a-zA-Z0-9\/\*-\\\\\\\\\\\\\\%\=\.]*/i'$1是ID完美的$article->text=preg\u replace$ychar,$this->youtubedeembed$1,$article->text;
$article=explode("?v=",$article);
$article=explode("\"",$article[1]);
$youtube_id=$article[0];
<?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;

?>
//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;
}