I have variable $post->post_content that return me detail of post and some video URL from YouTube.

I need to remove any text from $post->post_content else YouTube link, And add to YouTube likn this tag


the content like :-

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.


and some time like :-

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


How can do that.?

1 Answer 1


You can try doing this:

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

In your case would be something similar to this:

preg_match_all('@(https?://www\.?youtube.com/.[^\s.,"\']+)@i', $post->post_content, $matches);
foreach($matches as $match){
    echo "<video>$match</video>";
  • thank you, but i test it and not work with me, its not print any thing Jul 2, 2015 at 9:18
  • What's inside your post? How does it look like? The output of $post->post_content
    – marcovega
    Jul 2, 2015 at 9:20
  • i edit my post, see it Jul 2, 2015 at 9:36
  • I have edited my answer with a new regex code
    – marcovega
    Jul 2, 2015 at 9:40
  • its same old code :-) Jul 2, 2015 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.