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条)

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作