Php Dynamically Embed YouTube Videos

My page accepts any text in text area with no chars limit. I want to display youtube video in place of the "youtubeURL" or the direct embed code of youtube if the text data contains any of the above way.

example: If user enters following text into text area :

"Here is video by embed code: < iframe width="420" height="315" src="https://www.youtube.com/embed/7M-jsjLB20Y" frameborder="0" allowfullscreen>< /iframe > and here is the video by url: https://www.youtube.com/watch?v=7M-jsjLB20Y"

so post data will show in page like :

Here is video by embed code: "DISPLAY YOUTUBE VIDEO HERE" and here is the video by url: "DISPLAY YOUTUBE VIDEO HERE"

In short I want help to code Php page which accepts both way of youtube video embedding and find it from the text data then process it. It would be great help if anybody can give me idea which works with both way.


i don't really understand your question, but maybe with some regexp you can do what you want. For example, imagine you have your text area in a var like $l_sMyCode , you can extract the embed code like this :

preg_match( '/.*(<iframe.*</iframe>).*/i', $l_sMyCode, $l_aMatch );

And url like this :

preg_match( '/.*(https?.*youtube.*watch?v=.*) .*/i', $l_sMyCode, $l_aMatch );

You will get the match pattern in $l_aMatch[1] and you can then replace with a str_replace, or all in one regexp (you can find code on internet easily). Hope that will help you.

链接地址: http://www.djcxy.com/p/44764.html

上一篇: 嵌入YouTube HTML5视频

下一篇: Php动态嵌入YouTube视频