0
$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;

This is what I have so far ..and it works except it does not strip hrefs from the string that have youtube in it.

My desired output

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. joeCiY1MmZU 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. oddCiY1MmZU eauCiY1MmZU


Notice the ID's in the output ...no url's or href tags from youtube

Salman A
  • 262,204
  • 82
  • 430
  • 521
Joseph Philbert
  • 635
  • 7
  • 8

3 Answers3

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

$youtube_id will return the id of video

Akash Kumar
  • 642
  • 7
  • 20
0

See if this code will work.

Working 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;

?>

Output:

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. eauCiY1MmZU eauCiY1MmZU

Panama Jack
  • 24,158
  • 10
  • 63
  • 95
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;
}

}

After some digging and prodding this is a working solution and works so well. First preg_replace strips all href URLs for the giving domain then ... removes the rest in the other preg_replace

Joseph Philbert
  • 635
  • 7
  • 8