Web Hosting Talk







View Full Version : Space at end of string?!?


broshman
03-23-2004, 09:14 PM
Hi,
I am currently reading in proxies from a file, and some examples of proxie.txt are below:
200.241.56.130:80
200.115.207.125:6588
200.180.225.234:80
200.217.102.142:3128
200.170.1.230:8080

Now, what I am trying to do is seperate everything before the : to $host and everything after the : to $port

My code is below

$fp=fopen("proxies.txt", 'r') or die("Could not open proxies.txt!");
while(!feof($fp)){
$line=fgets($fp, 1024);
$pos=strpos($line, ':');
$host=substr($line, 0, $pos);
$port=substr($line, ($pos+1), (strlen($line)-1));
print "Port length: ".strlen($port)."<br>";
print "Host=$host<br> Port=$port<br>";
}

Now, the problem is, is that there is a space " " at the end of $host, which I have no idea where it is coming from. When I do Port length on the first proxy, 200.241.56.130:80, it says the length is 3 when really it is 2, because 80 is 2 digits.
I have tried millions of ways of getting rid of the space or putting everything except the space in a string, but no success.
If anyone has an idea why, please let me know!

sav
03-23-2004, 09:26 PM
Try something like this:

$ret = explode(":", trim($line));
$host = $ret[0];
$port = $ret[1];

null
03-23-2004, 10:46 PM
<?php

// get array of lines from file
$proxies = file("proxies.txt");

// could not open
if(!$proxies)
die("Could not open file proxies.txt!");

while(list(, $value)=each($proxies))
{
// split line into parts and delete spaces
$parts = split(":", trim($value))
$ips[] = $parts[0];
$ports[] = $parts[1];
}

?>


Array $ips will hold ip addresses and $ports will hold ports for those ips.

Enjoy :)

null

robeyh
03-24-2004, 01:44 AM
Just a personal preference thing... I would rather do (base code stolen from null):


<?php

// get array of lines from file
if(!$proxyfile = file("proxies.txt"))
die("Could not open file proxies.txt!");

$proxy = array();

while(list(, $value)=each($proxyfile)){
// split line into parts and delete spaces
$proxy[] = split(":", trim($value));
}

fclose($proxyfile);
?>


Then access host:ip using $proxy[i][0]:$proxy[i][1]

Guess I've seen too many designs using split storage go awry.

digitok
03-24-2004, 01:49 AM
You should be using explode, not split.

robeyh
03-24-2004, 02:01 AM
you're right, as always. No regex, no need to use split.