douse8732 2011-12-04 05:34
浏览 15

使用php从URL字符串中捕获值

I need to extract a variable's value from a string, which happens to be a URL. The string/url is loaded as part of a separate php query, not the url in the browser.

The url's will look like:

index.php?option=com_content&view=article&catid=334:golfeq&id=2773:xcelsiors&Itemid=44

How can I always find & capture the value of the id which in this example is 2773?

I read several examples already, but what I've tried captures the id value of the current page which is being viewed in the browser, and not the URL string.

Thanks

  • 写回答

3条回答 默认 最新

  • dongtaidai0492 2011-12-04 05:38
    关注

    You are looking for a combination or parse_url (which will isolate the query string for you) and parse_str (which will parse the variables and put them into an array).

    For example:

    $url = 'index.php?option=com_content&view=article&catid=334:golfeq&id=2773:xcelsiors&Itemid=44';
    
    // This parses the url for the query string, and parses the vars from that
    // into the array $vars (which is created on the spot).
    parse_str(parse_url($url, PHP_URL_QUERY), $vars);
    
    print_r($vars); // see what's in there
    
    // Parse the value "2773:xcelsiors" to isolate the number
    $id = reset(explode(':', $vars['id']));
    
    // This will also work:
    $id = intval($vars['id']);
    
    echo "The id is $id
    ";
    

    See it in action.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题