douji3426 2011-12-17 20:31
浏览 129
已采纳

php字符串替换不起作用

I am running a code which takes a youtube url as input and i am using str_replace on it.

$title = str_replace('watch?v=', 'embed/', $title);

but the replacement just wont happen. I keep getting the same string back.

The youtube url im using as of now is http://www.youtube.com/watch?v=iwQx9gw2NfM

what mistake am I making ? The the serch string is CLEARLY present in the url but it wont get replaced.

Or is there a better way to do this ?

EDIT: The code works fine... I was trying to replace the wrong variable

  • 写回答

1条回答 默认 最新

  • dongma2388 2011-12-17 20:34
    关注

    This code works fine:

    $title = "http://www.youtube.com/watch?v=iwQx9gw2NfM";
    
    $title = str_replace('watch?v=', 'embed/', $title);
    
    echo $title;  //=> http://www.youtube.com/embed/iwQx9gw2NfM
    

    See it work here on tehplayground

    You must have another mistake in your code. Please paste your context code for additional help.

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

报告相同问题?

悬赏问题

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