SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict AdRock952's Avatar
    Join Date
    Aug 2006
    Posts
    243
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problem with a function i've written

    I have written a function that needs a little twekaing because it's not working properly.

    I want to pass it the first part of a UK postcode and if the first 2 digits are letters take one path and if the first digit is a letter and the second is a number take the other path.

    The problem is that it's not taking any path

    PHP Code:
    function getPostcode($postcode) {

        if(
    is_numeric($postcode{1})) {
            
    $postcode substr($postcode01);
        }
        elseif(
    is_numeric($postcode{2})) {
            
    $postcode substr($postcode02);
        }
        if(
    is_numeric($postcode{1})) {
            
    $scotland = array('G');
            
    $northwest = array('L');
            
    $northeast = array('S');
            
    $midlands = array('B');
            
    $london = array('E''N''W');
        }
        elseif(
    is_numeric($postcode{2})) {
            
    $scotland = array('AB''DD''DG''EH''FK''HS''IV''KA''KW''KY''ML''PA''PH''TD''ZE');
            
    $ireland = array('BT');
            
    $northwest = array('BB','BL','CA','CH','CW','FY','IM','LA','M','OL','PR','SK','SY','TF','WA','WN');
            
    $northeast = array('BD''DH''DL''DN''HD''HG''HU''HX''LA''LS''NE''SR''TS''WF''YO');
            
    $midlands = array('CV''DE''DY''LE''NN''NG''ST''WS''WV');
            
    $anglia = array('AL''CB''CM''CO''EN''IG''IP''LU''MK''NR''PE''RM''SG''SS''WD');
            
    $london = array('EC''NW''SE''SW''WC');
            
    $southwest = array('BA''BH''BS''DT''EX''GL''HR''PL''TA''TQ''TR''WR');
            
    $southcentral = array('GU''GY''HA''HP''JE''OX''PO''RG''SL''SN''SO''SP''UB');
            
    $southeast = array('BN''BR''CT''CR''DA''KT''ME''RH''SM''TN''TW');
        }

        if(isset(
    $scotland)) {
            if(
    in_array($postcode$scotland)) {
                return 
    "scotland";
            }
        }
        if(isset(
    $ireland)) {
            if(
    in_array($postcode$ireland)) {
                return 
    "northern-ireland";
            }
        }
        if(isset(
    $northwest)) {
            if(
    in_array($postcode$northwest)) {
                return 
    "north-west";
            }
        }
        if(isset(
    $northeast)) {
            if(
    in_array($postcode$northeast)) {
                return 
    "north-east";
            }
        }
        if(isset(
    $midlands)) {
            if(
    in_array($postcode$midlands)) {
                return 
    "midlands";
            }
        }
        if(isset(
    $anglia)) {
            if(
    in_array($postcode$anglia)) {
                return 
    "anglia";
            }
        }
        if(isset(
    $london)) {
            if(
    in_array($postcode$london)) {
                return 
    "london";
            }
        }
        if(isset(
    $southwest)) {
            if(
    in_array($postcode$southwest)) {
                return 
    "south-west";
            }
        }
        if(isset(
    $southcentral)) {
            if(
    in_array($postcode$southcentral)) {
                return 
    "south-central";
            }
        }
        if(isset(
    $southeast)) {
            if(
    in_array($postcode$southeast)) {
                return 
    "south-east";
            }
        }
        else return 
    "****ed up";
        


  2. #2
    SitePoint Zealot Mattinblack's Avatar
    Join Date
    May 2009
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats because this :

    $postcode{1}

    Will not do anything.
    try,

    $c2=substr($postcode,1,1);

    if (is_numeric($c2){ #only one you need to test
    $postcode=substr($postcode,0,1);
    }else{
    $postcode=substr($postcode,0,2);
    }
    if (strlen($postcode)==1){

    scotland blah blah (single letter)

    }else{

    scotland blah blah (double letter)

    }

  3. #3
    SitePoint Zealot Darren Lewis's Avatar
    Join Date
    Jul 2002
    Location
    UK
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Problem is that you're replacing your $postcode string with a portion of itself, and then retesting that string portion to see if its numeric. The logic used to trim means that it won't be a numeric, so you always return '***ed up'.

    Try
    PHP Code:
    function getPostcode($postcode) { 

        if(
    is_numeric($postcode{1})) { 
            
    $postcode substr($postcode01); 
            
    $scotland = array('G'); 
            
    $northwest = array('L'); 
            
    $northeast = array('S'); 
            
    $midlands = array('B'); 
            
    $london = array('E''N''W'); 
        } 
        elseif(
    is_numeric($postcode{2})) { 
            
    $postcode substr($postcode02); 
            
    $scotland = array('AB''DD''DG''EH''FK''HS''IV''KA''KW''KY''ML''PA''PH''TD''ZE'); 
            
    $ireland = array('BT'); 
            
    $northwest = array('BB','BL','CA','CH','CW','FY','IM','LA','M','OL','PR','SK','SY','TF','WA','WN'); 
            
    $northeast = array('BD''DH''DL''DN''HD''HG''HU''HX''LA''LS''NE''SR''TS''WF''YO'); 
            
    $midlands = array('CV''DE''DY''LE''NN''NG''ST''WS''WV'); 
            
    $anglia = array('AL''CB''CM''CO''EN''IG''IP''LU''MK''NR''PE''RM''SG''SS''WD'); 
            
    $london = array('EC''NW''SE''SW''WC'); 
            
    $southwest = array('BA''BH''BS''DT''EX''GL''HR''PL''TA''TQ''TR''WR'); 
            
    $southcentral = array('GU''GY''HA''HP''JE''OX''PO''RG''SL''SN''SO''SP''UB'); 
            
    $southeast = array('BN''BR''CT''CR''DA''KT''ME''RH''SM''TN''TW'); 
        } 

        if(isset(
    $scotland)) { 
            if(
    in_array($postcode$scotland)) { 
                return 
    "scotland"
            } 
        } 
        if(isset(
    $ireland)) { 
            if(
    in_array($postcode$ireland)) { 
                return 
    "northern-ireland"
            } 
        } 
        if(isset(
    $northwest)) { 
            if(
    in_array($postcode$northwest)) { 
                return 
    "north-west"
            } 
        } 
        if(isset(
    $northeast)) { 
            if(
    in_array($postcode$northeast)) { 
                return 
    "north-east"
            } 
        } 
        if(isset(
    $midlands)) { 
            if(
    in_array($postcode$midlands)) { 
                return 
    "midlands"
            } 
        } 
        if(isset(
    $anglia)) { 
            if(
    in_array($postcode$anglia)) { 
                return 
    "anglia"
            } 
        } 
        if(isset(
    $london)) { 
            if(
    in_array($postcode$london)) { 
                return 
    "london"
            } 
        } 
        if(isset(
    $southwest)) { 
            if(
    in_array($postcode$southwest)) { 
                return 
    "south-west"
            } 
        } 
        if(isset(
    $southcentral)) { 
            if(
    in_array($postcode$southcentral)) { 
                return 
    "south-central"
            } 
        } 
        if(isset(
    $southeast)) { 
            if(
    in_array($postcode$southeast)) { 
                return 
    "south-east"
            } 
        } 
        else return 
    "****ed up"
         


    echo 
    getPostcode('SW1W'); 
    Should display "london"

    Does that help?
    Censura - Add reviews and ratings to your site

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mattinblack View Post
    Thats because this :

    $postcode{1}

    Will not do anything.
    try,
    It actually does work. php lets you access each character of a string using that syntax.
    http://www.php.net/manual/en/languag....string.substr

  5. #5
    SitePoint Zealot Mattinblack's Avatar
    Join Date
    May 2009
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    It actually does work. php lets you access each character of a string using that syntax.
    http://www.php.net/manual/en/languag....string.substr
    So it does...lol. for years I have been using square brackets $postcode[1] and that works too!


Bookmarks

Posting Permissions

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