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条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历