PHP:忽略og删除列表末尾的空行
我有一个PHP脚本,它从一个URL获取一个IP地址列表,并在其中添加一些文本和换行符。 这工作得很好,除了源代码末尾有一个空行,这使得一行没有IP地址。 我需要脚本忽略或删除该行,以便不生成该行PHP:忽略og删除列表末尾的空行,php,Php,我有一个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 "
<?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";
}
?>
您可以在此处看到结果和空的最后一行
<?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";
}
?>
或者,使用
内爆()
<?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);
?>
谢谢你的回答,我有以下工作要做:
<?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";
}
}
?>
爆炸后是否可以尝试使用array\u filter
?例如:$ipList=array\u过滤器(explode(“\n“,$ipPage))
。它将删除每个值等于FALSE的项,并且您只需检查$ip
var是否真实,比如foreach($ipList as$ip){if($ip){echo.…;}
,或者使用trim$ipPage=trim($ipPage)代码>。