drfu29983 2010-10-28 10:02
浏览 81
已采纳

从字符串的开头和结尾删除内容(PHP)

I'm trying to get a users ID from a string such as:

http://www.abcxyz.com/123456789/

To appear as 123456789 essentially stripping the info up to the first / and also removing the end /. I did have a look around on the net but there seems to be so many solutions but nothing answering both start and end.

Thanks :)

Update 1

The link can take two forms: mod_rewrite as above and also "http://www.abcxyz.com/profile?user_id=123456789"

  • 写回答

5条回答 默认 最新

  • dqzuo0327 2010-10-28 10:06
    关注

    I would use parse_url() to cleanly extract the path component from the URL:

    $path = parse_URL("http://www.example.com/123456789/", PHP_URL_PATH);
    

    and then split the path into its elements using explode():

    $path = trim($path, "/"); // Remove starting and trailing slashes
    $path_exploded = explode("/", $path);
    

    and then output the first component of the path:

    echo $path_exploded[0]; // Will output 123456789
    

    this method will work in edge cases like

    • http://www.example.com/123456789?test
    • http://www.example.com//123456789
    • www.example.com/123456789/abcdef

    and even

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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?