I have a site based on php/mySQL where publishers had inserted a lot of youtube videos in a specific database column copiyng/pasting youtube embed codes. There is a wide variance of the codes reflecting the variations google made during time. Different sizes of the player window is the main variation. Now I need to uniform all the windows, but I don't know where to start. What I need to reach is something like a regexp to eliminate all the not necessary code from the various entries (i.e. convert all embed codes in their corresponding youtube url?) and then create a script to generate the player code runtime... Some help to look where to start? Thank you!
1条回答 默认 最新
- dowb58485 2010-08-13 13:58关注
New Complete Solution:
<?php //your bad code which you want to sanitize $BadCode = '<object width="640" height="385"> <param name="movie" value="http://www.youtube.com/v/TvJsRX9SKUo?fs=1&hl=it_IT"></param> <param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param> <embed src="http://www.youtube.com/v/TvJsRX9SKUo?fs=1&hl=it_IT" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed> </object>'; //Get Youtube Video ID preg_match('#(?<=youtube\.com/v/)\w+#', $BadCode, $matches); $VideoID = $matches[0]; //New Correct and consistent embed code template. Create whatever consistent template you want. Then all your videos will follow this template. Change the place where video-id appears as [VIDEOID] $template = '<object type="application/x-shockwave-flash" style="width:450px; height:366px;" data="http://www.youtube.com/v/[VIDEOID]?showsearch=0&fs=1"> <param name="movie" value="http://www.youtube.com/v/[VIDEOID]?showsearch=0&fs=1" /><param name="allowFullScreen" value="true" /> </object>'; //Insert our VideoID into this template. $GoodCode = str_replace('[VIDEOID]', $VideoID, $template); //Display code for testing echo $GoodCode; //Update your database table (please change variables yourself) //mysql_query("update tablename set YoutubeCode = '".$GoodCode."' where ID={$ID}"); ?>
Old Summarized Answer:
[Regex for extracting video ID]. After getting video ID, you have the video ID. Use this ID to generate a clean consistent code for all your videos. Then, update the database.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 机器学习能否像多层线性模型一样处理嵌套数据
- ¥20 西门子S7-Graph,S7-300,梯形图
- ¥50 用易语言http 访问不了网页
- ¥50 safari浏览器fetch提交数据后数据丢失问题
- ¥15 matlab不知道怎么改,求解答!!
- ¥15 永磁直线电机的电流环pi调不出来
- ¥15 用stata实现聚类的代码
- ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
- ¥20 docker里部署springboot项目,访问不到扬声器
- ¥15 netty整合springboot之后自动重连失效