dongtiaobeng7901
2016-03-18 10:28
浏览 200
已采纳

从字符串中提取ip地址[重复]

This question already has an answer here:

I have following string an want to exctract the 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>

i want to extract that:

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

in a string in this format: 176.199.114.9

i have tried it with:

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; 

and my output just

Array
</div>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douyanti2808 2016-03-18 10:52
    已采纳

    if you get to a point where you have the IP mixed with a couple of tags, you could simple appy the php function strip_tags() to it:

    $ip = "176.<wbr>199.<wbr>114.<wbr>9";
    $cleanIp = strip_tags($ip); //output 176.199.114.9
    
    已采纳该答案
    打赏 评论
  • donglou8371 2016-03-18 10:49

    Try the regex: (\d*).<wbr>(\d*).<wbr>(\d*).<wbr>(\d*)

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

    Update I assume there will only be one IP on the site (i.e., the one you want). So we can replace:

    preg_match("/(\d*).<wbr>(\d*).<wbr>(\d*).<wbr>(\d*)/",$result,$ip);
    $ip = join('.', array_slice($ip, 1));
    echo $ip;
    
    打赏 评论
  • dtx9763 2016-03-18 11:10

    It would be much more convenient with a DOM parser, there may be some warnings related to your wbr tags

    $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
    }
    
    打赏 评论

相关推荐 更多相似问题