dqo88037 2013-04-30 16:10
浏览 45
已采纳

用于iframe变量的PHP preg_replace

I have a widget for users to enter in the code for an iframe widget that is created on another site. There are a few variations of what the iframe code that is generated from the other site will contain, so I want to strip out everything except for the 6 digits that identify the widget, and then echo back the iframe code with the digits inserted.

I'm not sure why this isn't working … The code I have been using is here:

<?php

$widget_id = $vars['entity']->widget_id;

if ($widget_id) {

    $widget_id = preg_replace('<iframe src=(.*?)/widgets/(\d+)(.*?)</iframe>', '$2', $widget_id);

    echo '<iframe src="http://website.com/widgets/'.$widget_id.'/player_universal" width="300" height="250"></iframe>';

}

else {
    echo "Enter the widget short code";
}
  • 写回答

1条回答 默认 最新

  • dongyi6543 2013-04-30 16:17
    关注

    This should work for you

    $widget_id = preg_replace('/\<iframe src=(.*?)\/widgets\/(\d+)(.*?)\<\/iframe\>/', '$2', $widget_id);
    

    notice the delimiters around the regular expression / reg-exp /
    you can make it case-insensitive too by using i modifier / reg-exp /i

    you also need to escape all the special reg-exp characters too (see preg_quote() for a list)

    See
    http://www.php.net/manual/en/function.preg-replace.php
    http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php

    p.s. / is not the only character you can use, I've seen @ and | too

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧