2015-07-02 57 views
-1

我有变量$post->post_content,可以返回来自YouTube的帖子和某个视频网址的详细信息。PHP:将视频标签添加到youtube链接fron内容

我需要删除从$post->post_content其他YouTube链接的任何文字,并添加到YouTube likn这个标签

<video>YouTube_URL</video> 

content,如: -

At this instant, while Daggoo, on the summit of the head, was clearing the whip—which had somehow got foul of the great cutting tackles—a sharp cracking noise was heard; and to the unspeakable horror of all, one of the two enormous hooks suspending the head tore out, and with a vast vibration the enormous mass sideways swung, till the drunk ship reeled and shook as if smitten by an iceberg. The one remaining hook, upon which the entire strain now depended, seemed every instant to be on the point of giving way; an event still more likely from the violent motions of the head. 

https://www.youtube.com/watch?v=JEreM0ZKY18 

一些时间,如: -

أسر شعار اسبوعين الدولارات من, شرسة أعلنت اندلاع إذ هذا. وبعد الثالث أوكيناوا ما بين, الحصار الأمامية بـ عدد. ذات بقعة فمرّ إذ, أخذ كل بالحرب وسمّيت المانيا. و فصل بمباركة المقيتة, أملاً الحصار المتاخمة من عدد. بزمام أثره، التبرعات تم بعد, الجيش خصوصا كانتا ان دار. 

    https://www.youtube.com/watch?v=JEreM0ZKY18 

我的代码:

$posts = query_posts('showposts='.$numposts.'&cat=19'); 
foreach ($posts as $post) { 
$postOutput1 = preg_replace('#<a[^>]+>([\r\n|\n]+)?<img[^>]+>([\r\n|\n]+)?<\/a>#','', $post->post_content); 
$postOutput2 = preg_replace('@https?://(www\.)?youtube.com/.[^\s.,"\'][email protected]','', $postOutput1); 
$postOutput = preg_replace('[youtube','', $postOutput2); 
preg_match_all('@https?://(www\.)?youtube.com/.[^\s.,"\'][email protected]', $post->post_content, $matches); 
foreach($matches as $match){ 
    echo "<video>$match</video>"; 
}} 

该怎么办?

+0

什么变量是什么样子?你能粘贴它的内容吗? – Vlad

+0

@Vlad我编辑我的文章 –

回答

0

试试这个:

$string = "At this instan ... https://www.youtube.com/watch?v=JEreM0ZKY18" 
$pattern = '/(http\S*)/im'; 
$array = array(); 
preg_match($pattern, $string, $array); 
$link = $array[0]; 
print $link;