douque9982 2010-06-26 21:40
浏览 39
已采纳

如何使用正则表达式获取行尾的信息?

For example,

http://api.hostip.info/get_html.php?ip=12.215.42.19

When visiting that URL, it will return:

Country: UNITED STATES (US)
City: Sugar Grove, IL
IP: 12.215.42.19

I want to set Sugar Grove, IL to a variable, after visiting that URL.

How would I make it find the City, and then set it to a variable?

  • 写回答

2条回答 默认 最新

  • doutang6600 2010-06-26 21:43
    关注

    This will do the trick:

    <?php
    $page = "Country: UNITED STATES (US)
    City: Sugar Grove, IL
    IP: 12.215.42.19
    ";
    
    preg_match( '/City:\s*([^
    ]+)/', $page, $matches );
    
    echo $matches[1];
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?