doujiao1984 2019-05-23 15:12
浏览 210


I've this code here:

$url = '';

echo substr( $url, strrpos( $url, '/' ) + 1 );

This returns me the needed id:


Now the problem begins. In some cases the URL looks like this:

$url = '';

$url = '';

This means that my code don't works anymore. So is there any way to be 100 % sure that I always get my ID from the URL? The ID is always at the same position from the beginning on but the end can be different.


When I've this code here I'm getting not the last part anymore. Any idea why and how to fix this?:

$url = '';
$id = basename( dirname( $url ) );

I just want to be sure that it works in every situation. In this case the selected part is:

  • 写回答

2条回答 默认 最新

  • douxun7992 2019-05-23 15:17

    You could use functions that are meant for directories and/or URLs:

    echo basename(dirname($url));
    echo basename(pathinfo($url, PATHINFO_DIRNAME));
    echo basename(parse_url($url, PHP_URL_PATH));

    The last one may return the filename if you had so you would want to use:

    echo basename(dirname(parse_url($url, PHP_URL_PATH)));

    Lot's of possibilities depending on what you need. The point is that there are specific functions for working with directories, filenames and URLs so that you don't have to treat them as just strings that have no meaning but unlimited possibilities..

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



  • ¥15 C++/QT设置函数调用条件(多个功能调用同一,且各自调用条件不同)
  • ¥15 UE5样条线生成的网格UV问题
  • ¥15 如何用最短的时间大致看懂springboot+vue的项目
  • ¥15 (有偿)懂数值分析和含时变参数微分方程的来
  • ¥15 abaqus随机生成二维颗粒
  • ¥15 安装ansys许可证管理器时出现了这个问题,如何解决?
  • ¥100 高价求算法,利用智能手机传感器计算车辆的三轴g值
  • ¥15 Blazor server 数据库操作异常,如何解决?(语言-c#)
  • ¥15 uni-app开发APP运行到浏览器访问接口跨域
  • ¥100 mfc消息自创建控件