Results 1 to 6 of 6
  1. #1

    Space at end of string?!?

    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!

  2. #2
    Join Date
    Jun 2001
    Location
    Partizansk, Russia
    Posts
    74

    Re: Space at end of string?!?

    Try something like this:

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

  3. #3
    Join Date
    Sep 2002
    Location
    Illinois
    Posts
    2,304
    PHP Code:
    <?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

  4. #4
    Join Date
    Mar 2004
    Location
    Granville OH
    Posts
    108
    Just a personal preference thing... I would rather do (base code stolen from null):

    PHP Code:
    <?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.

  5. #5
    Join Date
    Jan 2003
    Location
    Perth, WA, Australia
    Posts
    1,276
    You should be using explode, not split.
    nu-metal.org :: coming soon

  6. #6
    Join Date
    Mar 2004
    Location
    Granville OH
    Posts
    108
    you're right, as always. No regex, no need to use split.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •