drze7794 2013-05-21 15:00
浏览 50
已采纳

使用PHP提取特定的字符串数据

The iframe is assigned to a variable $string. I want extract the texts between embed/ and ?rel in the iframes src. So i would be looking for this code sL-2oYAI35o

<iframe width="420" height="315" src="http://www.youtube.com/embed/sL-2oYAI35o?rel=0" frameborder="0" allowfullscreen></iframe>

Would I need to use a php substr function or regex? I cant use the regular substring because the start value may differ.

  • 写回答

2条回答 默认 最新

  • donglan9651 2013-05-21 15:06
    关注
    $text = '<iframe width="420" height="315" src="http://www.youtube.com/embed/sL-2oYAI35o?rel=0" frameborder="0" allowfullscreen></iframe>';
    
    preg_match('`embed/(.+)\?rel`i', $text, $matches);
    $code = (!empty($matches[1])) ? $matches[1] : '';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部