简体   繁体   English

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

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

For example, 例如,

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

When visiting that URL, it will return: 访问该URL时,它将返回:

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. 访问该URL后Sugar Grove, IL我想将Sugar Grove, ILSugar Grove, IL设置为变量。

How would I make it find the 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 当然,我们在这里使用eval(),因此仅当您100%确定数据完整性时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM