开发者

How can I use regex to grab the information on the END of a line?

开发者 https://www.devze.com 2023-01-05 01:17 出处:网络
For example, http://api.hostip.info/get_html.php?ip=12.215.42.19 When visiting that URL, it will return:

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 t开发者_开发知识库he City, and then set it to a variable?


This will do the trick:

<?php
$page = "Country: UNITED STATES (US)
City: Sugar Grove, IL
IP: 12.215.42.19
";

preg_match( '/City:\s*([^\n]+)/', $page, $matches );

echo $matches[1];
?>


$x=<<<CODE
Country: UNITED STATES (US)
City: Sugar Grove, IL
IP: 12.215.42.19
CODE;

preg_replace('`\nCity:\s(.*)\r`Ue','$city="$1";',$code);

Of course, we are using eval() here, so only if you are 100% sure of data integrity

0

精彩评论

暂无评论...
验证码 换一张
取 消