从内容向YouTube链接添加视频标签

时间:2015-07-02 作者:Zamalek Man

我有变量$post->post_content 这会给我返回帖子的详细信息和YouTube上的一些视频URL。

我需要删除$post->post_content else YouTube链接,并添加到YouTube,如此标记

<video>YouTube_URL</video>
thecontent 例如:-

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
怎么能做到这一点。?

1 个回复
SO网友:marcovega

您可以尝试这样做:

$string =  "Some text before the link http://www.youtube.com/?123 some text after the link";
preg_match_all(\'@(https?://www\\.?youtube.com/.[^\\s.,"\\\']+)@i\', $string, $matches);
var_dump($matches);
在您的情况下,类似于此:

preg_match_all(\'@(https?://www\\.?youtube.com/.[^\\s.,"\\\']+)@i\', $post->post_content, $matches);
foreach($matches as $match){
    echo "<video>$match</video>";
}

结束