dsfe167834 2011-12-05 02:26
浏览 60
已采纳

我怎样才能抓住我想要的文字 - PHP [复制]

This question already has an answer here:

So I've got this RSS file that I'm trying to get part of a URL from. So here's what I tried (which is not working).

I've got this URL I can get easily enough:

http://www.youtube.com/watch?v=tUPjxGmh9i8&feature=youtube_gdata

I tried doing an $link = ltrim($link, 'http://www.youtube.com/watch?v='); in PHP and an $link = rtrim($trim, '&feature=youtube_gdata');

And it returned "UPjxGmh9i8". It cuts off the "t" in the front. The PHP.net documentation pages aren't the easiest for me to read and interpret, but I'm assuming that any individual character within the second parameter of ltrim() and rtrim() is taken out and this is not what I want. Is there some other solution I can use to grab only the text I want?

Any help is greatly appreciated.

</div>
  • 写回答

2条回答 默认 最新

  • du5407 2011-12-05 02:29
    关注

    This is how I would do it...

    $query = parse_url(
                'http://www.youtube.com/watch?v=tUPjxGmh9i8&feature=youtube_gdata', 
                PHP_URL_QUERY);
    
    parse_str($query, $params);
    
    $slug = get_magic_quotes_gpc() ? stripslashes($params['v']) : $params['v'];
    

    CodePad.

    The reason trim() (or its variants) won't work is because it accepts a character list (ordinal not significant), of which http://www.youtube.com/watch?v= contains t. It does not accept a string to remove.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题