douya2007 2016-03-28 08:28
浏览 61
已采纳

从“gethostbyaddr”字符串中提取主机名

I wish to extract the exact hostname from an ip address but what i am receiving is an entire string of the address.

I would just like to extract only the Host Name from the the string. Can any one point me in the right direction ?

Here is the snippet of the code being used:-

<?php

    $ip = $_SERVER['REMOTE_ADDR'];
    $fullhost = gethostbyname(gethostbyaddr($ip));
    $host = preg_replace("/^[^.]+./", "*.", $fullhost);
?>

Host: <?=$host?>

The output received from it is :-

Host: *.36.64.182.airtelbroadband.in

I would just like to display Airtel Broadband and nothing else to the user

</div>
  • 写回答

2条回答 默认 最新

  • doulang7699 2016-03-28 08:53
    关注

    You can try this:

    $host      = gethostbyaddr($_SERVER['REMOTE_ADDR']); // gets the full hostname
    $hostNames = explode(".", $host);      // explodes into parts divided by DOT
    echo $hostNames[count($hostNames)-2]; // take what you need
    // $hostNames[count($hostNames)-1];   -> in
    // $hostNames[count($hostNames)-2];   -> airtelbroadband
    

    You might also want to look into geoip.
    This is a basic PHP extension that can be installed using PECL.

    string geoip_isp_by_name ( string $hostname )
    The geoip_isp_by_name() function will return the name of the Internet Service Provider (ISP) that an IP is assigned to.

    This function is currently only available to users who have bought a commercial GeoIP ISP Edition. A warning will be issued if the proper database cannot be located.

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

报告相同问题?

悬赏问题

  • ¥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 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看