tomindo Posted December 15, 2010 Share Posted December 15, 2010 could someone help me with this code? It works fine without ip2long function? weird <form method="post" action="test.php"> <textarea name="new" cols="40" rows="10"> samplehost-1,127.0.0.1 samplehost-2,127.0.0.2 </textarea><br> <input type="submit" value="Add Servers"> </form> if(isset($_POST['new'])) { $array= explode("\n",$_POST['new']); foreach ($array as $ni) { $array=explode(',', $ni); echo ip2long($array[1]); } } Quote Link to comment Share on other sites More sharing options...
scotmcc Posted December 15, 2010 Share Posted December 15, 2010 Looks to me like you're probably getting a (bool) false back from these addresses, which will show up as nothing in HTML. Try this: <form method="post" action="<?php echo $PHP_SELF;?>"> <textarea name="new" cols="40" rows="10"> samplehost-1,127.0.0.1 samplehost-2,127.0.0.2 </textarea><br> <input type="submit" value="Add Servers"> </form> <?php if(isset($_POST['new'])) { $array = explode("\n",$_POST['new']); foreach ($array as $ni) { $test = explode(',', $ni); echo ip2long(trim($test[1]))."<br/>"; } } ?> I just threw a trim() around the item. I also changed your second variable to '$test' as it looked to me like you were overwriting your original $array with the second explode... but I didn't look at it long enough to be sure. Just make sure you're grabbing an IP address. You might think about using a regular expression... something like this: <form method="post" action="<?php echo $PHP_SELF;?>"> <textarea name="new" cols="40" rows="10"> samplehost-1,127.0.0.1 samplehost-2,127.0.0.2 samplehost-3,999.999.999.999</textarea><br> <input type="submit" value="Add Servers"> </form> <?php if(isset($_POST['new'])) { $RE = "/(??:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/"; foreach (explode("\n",$_POST['new']) as $address) { $data = array(); if (preg_match($RE, $address, $data)) { var_dump($data); echo $address." = ".ip2long($data[0])."<br />"; } else { echo "Not a valid address!"; } } } ?> Quote Link to comment Share on other sites More sharing options...
tomindo Posted December 15, 2010 Author Share Posted December 15, 2010 thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.