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

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

图片转代码服务由CSDN问答提供 功能建议

我正在运行一个代码,它以youtube url作为输入,我正在使用str_replace。

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

但是 更换不会发生。 我一直收到同样的字符串。

我现在使用的youtube网址是 http://www.youtube.com/watch?v=iwQx9gw2NfM

我犯了什么错误? 网址字符串清晰地显示在网址中,但它不会被替换。

或者有更好的方法吗?

编辑: 代码工作正常......我试图替换错误的变量

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题