PHP: 将视频标签添加到 youtube link 内容

PHP: Add video tag to youtube link fron content

我有变量 $post->post_content return 我 post 的详细信息和来自 YouTube 的一些视频 URL。

我需要从 $post->post_content else YouTube link 中删除任何文本,并添加到 YouTube 链接此标签

<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.,"\']+@i','', $postOutput1);
$postOutput = preg_replace('[youtube','', $postOutput2);
preg_match_all('@https?://(www\.)?youtube.com/.[^\s.,"\']+@i', $post->post_content, $matches);
foreach($matches as $match){
    echo "<video>$match</video>";
}}

怎么做到的?

试试这个:

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