2017-09-18 11 views
0

URLからIPアドレスのリストを取得し、その前にテキストと改行を付加するPHPスクリプトがあります。 これはうまくいきます。ただし、ソースには最後に空白行があり、IPアドレスなしの行になります。 スクリプトを生成しないように、その行を無視または削除するスクリプトが必要です。PHP:リストの終わりで空行を削除しないでください

<?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"; 
    } 
?> 

あなたは結果とhttps://novaram.dk/mikrotik.php

+1

おそらく、爆発の直後に 'array_filter'を試してみてください。 '$ ipList = array_filter(explode(" \ n "、$ ipPage))'のようになります。 – Soaku

+0

空の配列要素を削除する(https://stackoverflow.com/questions/3654295/remove-empty-array-elements) – Maraboc

+0

また、あなたは、 '$ ip' varは' foreach($ ipList as $ ip){if($ ip){echo "....";}のように真実です。 }} 'を使うか、trim' $ ipPage = trim($ ipPage); 'を使ってください。 – Parziphal

答えて

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"; 
    } 
?> 
1

動作し、それらを結合するためにimplode()を使用する必要があります。

<?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"; 
    } 
} 
?> 

...しかし、私は同様のスクリプトを持っていますが、ソースは私に私がする必要があるリストを送信します。

<?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); 
?> 
0

は、私が仕事に次しまった、すべてのあなたの答えをありがとう最初の行を無視しますか?

関連する問題