drpfu51608120170 2013-01-09 21:05
浏览 48
已采纳

拿网址并解析其中的一部分[重复]

Possible Duplicate:
Getting domain name without TLD

So I have a Web site I am trying to work on. In the URL there is info and I want to parse it out and use it to make a db call to mysql:

http://www.relevant-info.org/home.html

I want to take the Url put it into a string.

Then I want to only get 'relevant-info' out of the url.

$webaddress = $_SERVER["SERVER_NAME"];
$parts = parse_url($webaddress);
$url = substr($Parts, 4, -4);

^ i am using that and I got the ip address (it is a centos 5.8 lamp) on a VM i am testing this, so in short this seams to work good so far :)

  • 写回答

3条回答 默认 最新

  • dqtdz08206 2013-01-09 21:15
    关注

    Credits to Relentless

    You can easily find it with the PHP parse-url function: parse_url

    Example usage: If you want to get 'relevant-info', a PHP 5.4 valid code could look like:

    $Url = substr(parse_url("http://www.relevant-info.org/home.html")[1], 4, -4);
    

    Output

    relevant-info

    For PHP 5.3 and less

    $Parts = parse_url("http://www.relevant-info.org/home.html");
    $Url = substr($Parts, 4, -4);
    

    In future situtations, if you do not have fancy functions like parse-url, you can use Regex or use str_split

    Remember that str_split requires less resources

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看