从字符串中提取ip地址

时间:2016-03-18 10:28:41

标签: php

我有以下字符串想要提取ip-address:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="Hier k&ouml;nnen Sie sich Ihre eigene IP Adresse anzeigen lassen, sehen &uuml;ber welchen Host Sie surfen, woher Sie kommen, welchen Browser Sie nutzen und &uuml;ber welchen Link Sie zu dieser Seite gefunden haben.">
                <link rel="icon" href="/favicon.ico">

        <title>Meine IP Adresse ermitteln &amp; anzeigen &ndash; Geokodierung, Host, Herkunft, Browserkennung (User Agent) &amp; Referer</title>

        <link href="https://fonts.googleapis.com/css?family=Bree+Serif" rel="stylesheet" type="text/css">
        <link href="/css/font-awesome.min.css" rel="stylesheet">
        <link href="/css/bootstrap.min.css" rel="stylesheet">
        <link href="/css/styles.css" rel="stylesheet">
        <link href="/css/dark-bottom.min.css" rel="stylesheet">

        <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
        <!--[if lt IE 9]>
          <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
          <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>

    <body>
        <script>
          (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
          (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
          })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

          ga('create', 'UA-6321715-14', 'auto');
          ga('require', 'linkid');
          ga('require', 'displayfeatures');
          ga('send', 'pageview');
        </script>

        <div class="container">
            <div class="header clearfix">
                <h3>ueber-mich.com</h3>
                <nav>
                    <ul class="nav nav-pills pull-right">
                        <li class="active"><a href="/">Startseite</a></li>
                        <li>
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Glossar <span class="caret"></span></a>
                            <ul class="dropdown-menu">
                                <li class=""><a href="/ip-adresse/">IP-Adresse</a></li>
                                <li class=""><a href="/hostname/">Hostname</a></li>
                                <li class=""><a href="/whois/">Whois</a></li>
                                <li class=""><a href="/user-agent/">User-Agent</a></li>
                                <li class=""><a href="/referrer/">Referrer</a></li>
                                <li class=""><a href="/bildschirmaufloesung/">Bildschirmaufl&ouml;sung</a></li>
                                <li class=""><a href="/pixeldichte/">Pixeldichte</a></li>
                                <li class=""><a href="/farbtiefe/">Farbtiefe</a></li>
                            </ul>
                        </li>
                        <li class=""><a href="/datenschutz/">Datenschutz</a></li>
                        <li class=""><a href="/impressum/">Impressum</a></li>
                    </ul>
                </nav>
            </div>

<div class="jumbotron">
            <span class="label label-info"><a href="/ip-adresse/">IP-Adresse (IPv4)</a></span><strong class="ip">176.<wbr>199.<wbr>114.<wbr>9</strong>
                <span class="label label-hostname"><a href="/hostname/">Hostname</a></span><span class="hostname">ip-<wbr>176-<wbr>199-<wbr>114-<wbr>9.<wbr>hsi06.<wbr>unitymediagroup.<wbr>de</span>
                <br>        <span class="label label-hostname"><a href="/whois/">Whois</a></span><span class="whois">Unitymedia dynamic customer IP pool (Unitymedia Group)</span>
    </div>

<div class="jumbomap" style="background-image:url('map/?lng=8.0885&lat=51.5602&z=9')">
    <div class="location">
                    <span class="label label-info">Herkunft</span>
            <img src="img/flags/de.png">        Deutschland &raquo; Nordrhein-Westfalen &raquo; Soest   </div>
</div>

<div class="row">
    <div class="col-lg-12">
        <table>
            <tbody>
                <tr>
                    <th><span class="label label-info"><a href="/user-agent/">User-Agent</a></span></th>
                    <td>Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; fr; rv:1.7) Gecko/20040624 Firefox/0.9</td>
                </tr>
                <tr>
                    <th><span class="label label-info"><a href="/referrer/">Referrer</a></span></th>
                    <td>http://twitter.de</td>
                </tr>
                <tr>
                    <th><span class="label label-info"><a href="/bildschirmaufloesung/">Bildschirm&shy;aufl&ouml;sung</a></span></th>
                    <td class="bildschirmaufloesung"><span class="muted">&mdash;</span></td>
                </tr>
                <tr>
                    <th><span class="label label-info"><a href="/pixeldichte/">Pixeldichte</a></span></th>
                    <td class="pixeldichte"><span class="muted">&mdash;</span></td>
                </tr>
                <tr>
                    <th><span class="label label-info"><a href="/farbtiefe/">Farbtiefe</a></span></th>
                    <td class="farbtiefe"><span class="muted">&mdash;</span></td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

<div class="row intro">
    <div class="col-lg-12">
        <h1>Die eigene IP-Adresse ermitteln &amp; anzeigen</h1>
        <p>ueber-mich.com hilft dabei, Ihre eigene IP Adresse und weitere Details (Host, User-Agent, Referer, Auflösung, Farben) über Ihren Computer und dessen Aufenthaltsort herauszufinden und anzuzeigen.</p>
    </div>
</div>

            <footer class="footer text-center">
                <p>&copy; 2006&ndash;2016 <a href="http://www.udg.de/koeln">UDG K&ouml;ln</a> GmbH</p>
            </footer>

        </div>

        <script src="/js/ie10-viewport-bug-workaround.js"></script>
        <script src="/js/jquery.min.js"></script>
        <script src="/js/bootstrap.js"></script>
        <script src="/js/scripts.js"></script>
        <script src="/js/cookieconsent.min.js"></script>
    </body>
</html>

我想提取一下:

176.<wbr>199.<wbr>114.<wbr>9

在此格式的字符串中:176.199.114.9

我尝试过:

preg_match_all("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}.\d{1,3}/",$result,$ip);
echo $ip; 

和我的输出只是

Array

3 个答案:

答案 0 :(得分:2)

如果你到达了一个IP混合了几个标签的点,你可以简单地将php函数strip_tags()应用到它:

$ip = "176.<wbr>199.<wbr>114.<wbr>9";
$cleanIp = strip_tags($ip); //output 176.199.114.9

答案 1 :(得分:1)

试用正则表达式:(\d*).<wbr>(\d*).<wbr>(\d*).<wbr>(\d*)

preg_match_all("/(\d*).<wbr>(\d*).<wbr>(\d*).<wbr>(\d*)/",$result,$ip);
print_r($ip); 

<强>更新 我假设网站上只有一个IP(即你想要的那个)。 所以我们可以替换:

preg_match("/(\d*).<wbr>(\d*).<wbr>(\d*).<wbr>(\d*)/",$result,$ip);
$ip = join('.', array_slice($ip, 1));
echo $ip;

答案 2 :(得分:1)

使用DOM解析器会更方便,可能会有一些与wbr标签相关的警告

$r = YOUR_HTML_CONTENT
$doc = new DOMDocument();
$doc->loadHTML($r, LIBXML_NOWARNING);

$xpath = new DOMXpath($doc);
$class = $xpath->query("//*[contains(concat(' ', normalize-space(@class), '   '), ' ip ')]");
foreach($class as $item) { // DOMElement Object
 echo $item->nodeValue; //176.199.114.9
}