dtd14883 2019-01-17 21:29
浏览 31

在php [duplicate]中将长字符串变量的一小部分分配给另一个

This question already has an answer here:

I am new to php. Suppose I have a string variable as followings:

$val="https://www.example.com?id=1001";

Is there any way to get the value of the id part in another variable $id? So I just want to assign 1001 into $id using variable $val; How will I do it?

</div>
  • 写回答

1条回答 默认 最新

  • du0173 2019-01-17 21:31
    关注

    Parse the URL to get the query string id=1001 and then parse that to turn it into an array of GET parameters and there values:

    parse_str(parse_url($val, PHP_URL_QUERY), $result);
    echo $result['id'];
    

    This may seem like overkill for a string, but for variable URLs that may have a lengthy query string it should always work as the functions are designed for this purpose.

    If this were the URL of the actual page running the PHP then it is already populated into a super global:

    echo $_GET['id'];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助