我有一个PHP脚本,它从URL中获取IP地址列表,预先添加并附加一些文本和换行符。 这很有效,除了源在末尾有一个空行,这使得一行没有IP地址。 我需要脚本忽略或删除该行,以便不生成它。
<?php
$ipPage = file_get_contents('https://my.pingdom.com/probes/ipv4');
$ipList = explode("\n", $ipPage);
echo "/ip firewall address-list\n";
echo "remove [/ip firewall address-list find list=Pingdom]\n";
foreach($ipList as $ip) {
echo "add address=" . $ip . " list=Pingdom\n";
}
?>
看到结果和空的最后一行
答案 0 :(得分:1)
或者,使用implode()
加入他们。
<?php
$ipPage = file_get_contents('https://my.pingdom.com/probes/ipv4');
$ipList = explode("\n", $ipPage);
echo "/ip firewall address-list\n";
echo "remove [/ip firewall address-list find list=Pingdom]\n";
foreach($ipList as $ip) {
$ips[] = "add address={$ip} list=Pingdom";
}
echo implode("\n", $ips);
?>
答案 1 :(得分:0)
这个
<?php
$ipPage = file_get_contents('https://my.pingdom.com/probes/ipv4');
$ipList = preg_split ( "\n", $ipPage , -1 ,PREG_SPLIT_NO_EMPTY );
echo "/ip firewall address-list\n";
echo "remove [/ip firewall address-list find list=Pingdom]\n";
foreach($ipList as $ip) {
echo "add address=" . $ip . " list=Pingdom\n";
}
?>
或者这应该有用
<?php
$ipPage = file_get_contents('https://my.pingdom.com/probes/ipv4');
$ipList = explode("\n", $ipPage);
echo "/ip firewall address-list\n";
echo "remove [/ip firewall address-list find list=Pingdom]\n";
foreach($ipList as $ip) {
if(strlen(trim($ip)) != 0 )
echo "add address=" . $ip . " list=Pingdom\n";
}
?>
答案 2 :(得分:0)
感谢您的所有答案,我完成了以下工作:
<?php
$ipPage = file_get_contents('https://my.pingdom.com/probes/ipv4');
$ipPage = str_replace("'","",$ipPage);
$ipList = explode("\n", $ipPage);
echo "/ip firewall address-list\n";
echo "remove [/ip firewall address-list find list=Pingdom]\n";
foreach($ipList as $ip) {
if(strlen($ip) > 0) {
echo "add address=" . $ip . " list=Pingdom\n";
}
}
?>
...但是我有一个类似的脚本,但是源代码向我发送了一个我需要忽略第一行的列表?