SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict
    Join Date
    Mar 2011
    Location
    Manchester, UK
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Split PHP Variable Value - Help

    Hi Guys,

    Right guys i need some help and I've been thinking about this for some time and cant come up with a way round the issue. I have a form which consumers fill in, they need to enter their UK Post Code. Now i need to send that info onto our client but they require the postcode be sent in 2 strings. For example:

    Postcode=WA28PR

    SPLIT TO:

    Postcode1=WA2
    Postcode2=8PR

    Now there's different formats a UK Post Code can be:

    Format Example Coverage
    A9 9AA M1 1AA B, E, G, L, M, N, S, W postcode areas
    A99 9AA B33 8TH
    AA9 9AA CR2 6XH All postcode areas except B, E, G, L, M, N, S, W, WC
    AA99 9AA DN55 1PT
    A9A 9AA W1A 1HQ E1W, N1C, N1P, W1 postcode districts (high density areas where codes ran out)
    AA9A 9AA EC1A 1BB WC postcode area; EC1–EC4, NW1W, SE1P, SW1 postcode districts (high density areas where codes ran out)
    As you can see the different formats, now the issue i have is how do i go about working out where I should split the postcode value? The obvious thing for me to do would be to ask people to enter their first part and second part of their postcode in 2 different fields but I don't really want to do it that way as it may confuse consumers.

    Can anyone help me out please?

    Thank you...
    You're Help Does Not Go Unnoticed, I have So Far Donated £25 GBP
    To Cancer Research UK As A Thank You To All The SitePoint
    Members That Have Helped Me In The PHP Forum Thank You!

  2. #2
    SitePoint Evangelist smftre's Avatar
    Join Date
    Dec 2008
    Location
    London
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not have 2 input fields next to each other?
    Statvoo.com The Website Traffic Monitor
    The best way to monitor traffic to your sites for free!


    Web Development London UK We make web 3.0 applications

  3. #3
    SitePoint Addict
    Join Date
    Mar 2011
    Location
    Manchester, UK
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by smftre View Post
    Why not have 2 input fields next to each other?
    As that will confuse the consumer if theres 2 fields saying Postcode1 and Postcode2 which means conversion rates will be low.
    You're Help Does Not Go Unnoticed, I have So Far Donated £25 GBP
    To Cancer Research UK As A Thank You To All The SitePoint
    Members That Have Helped Me In The PHP Forum Thank You!

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The last 3 characters are always the final part of the post code.

    Lop them off, and what remains should be the first part -- does that pass all tests?

    W1A1AA

    So you might be sent a valid looking string -- but you (and your end client) might be more interested in trying to validate that you have been sent a real postcode before saving:

    Try using one of these postcode api uk

  5. #5
    SitePoint Addict
    Join Date
    Mar 2011
    Location
    Manchester, UK
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    The last 3 characters are always the final part of the post code.

    Lop them off, and what remains should be the first part -- does that pass all tests?

    W1A1AA

    So you might be sent a valid looking string -- but you (and your end client) might be more interested in trying to validate that you have been sent a real postcode before saving:

    Try using one of these postcode api uk
    Haha glad someone spotted this! Thank you....!

    How do i go about splitting the value of postcode into postcode1 and postcode2 with postcode2 having the last 3 values and postcode1 having the remaining?

    I know how to do it if theres ---- in between them as id just use this:

    PHP Code:
    list($postcode1$postcode2) = explode("----"$postcode); 
    Any help would be great.

    Thanks
    You're Help Does Not Go Unnoticed, I have So Far Donated £25 GBP
    To Cancer Research UK As A Thank You To All The SitePoint
    Members That Have Helped Me In The PHP Forum Thank You!

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    start with substr()

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    Specifically, note that the start parameter of substr can carry a negative number.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  8. #8
    SitePoint Addict
    Join Date
    Mar 2011
    Location
    Manchester, UK
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Guys,

    Thanks for the help, sorry for not responding sooner i never got an emailing there was a new reply. I've managed to code what i need it do to but i have an issue, below is my code:

    PHP Code:
    $postcode=$_GET['postcode'];

    function 
    countchar ($string) { 
        
    $resultpostcode strlen ($string)  -   substr_count($string' '); 
    echo 
    $resultpostcode;  


    countchar ($postcode);


    if ( 
    $resultpostcode == ) {
            
            echo 
    "Postcode1 ";
            echo 
    substr($postcode03);
            echo 
    "<br>";
            echo 
    "Postcode2 ";
            echo 
    substr($postcode3);

    } elseif( 
    $resultpostcode == ){

            echo 
    "Postcode1 ";
            echo 
    substr($postcode04);
            echo 
    "<br>";
            echo 
    "Postcode2 ";
            echo 
    substr($postcode4);

    }else {
        echo 
    "error";

    If the value of postcoderesult is 6 (it even outputs 6) then it wont echo what it should be doing, the same if its 7. It just keeps echoing Error.

    Any help would be great please.

    Thank you.
    You're Help Does Not Go Unnoticed, I have So Far Donated £25 GBP
    To Cancer Research UK As A Thank You To All The SitePoint
    Members That Have Helped Me In The PHP Forum Thank You!

  9. #9
    SitePoint Addict
    Join Date
    Mar 2011
    Location
    Manchester, UK
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bump. Any help would be great.

    Thanks
    You're Help Does Not Go Unnoticed, I have So Far Donated £25 GBP
    To Cancer Research UK As A Thank You To All The SitePoint
    Members That Have Helped Me In The PHP Forum Thank You!

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Lets say for now that the test values which should all pass are held in this array:

    PHP Code:
    $a= array(
    "WA28PR"
    "WA2 8PR",
    "WA2 8PR ",
    "WA2-8PR ",
    "(wa2 8PR"
    ); 
    ...and the result must each equal array("WA2", "8PR");

    You need to create a function which

    a) removes all of the spaces
    b) removes anything which is not a permitted letter or a permitted number
    c) uppercases what is left
    d) checks the length is valid
    // else return false
    e) takes of the last 3 chars
    f) isolates the remaining chars
    g) returns an array with the previous 2 values

    Why don't you give it your best shot at running some code against that test array and see how close you can get, then ask for help if you get stuck.

    here's a start at a) we dont validate vs the final array for the time being, just against the expected result: "WA28PR"
    PHP Code:
    function checkPostcode($pc){

    $pc str_replace(" """$pc);

    return 
    $pc;
    }

    foreach(
    $a as $pcode){
    $new checkPostcode($pcode);

    if( 
    $new === "WA28PR"){
    echo 
    "PASS ! ";
    }
    echo 
    checkPostcode($pcode) , PHP_EOL;
    }


    // gives
    WA28PR PASS !
    WA28PR PASS !
    WA28PR PASS !
    WA2-8PR
    (wa28PR 
    So, what's next what do you add to the function to get more passes?
    What can you add to the original array to make your tests more lifelike?
    You could create a new array of values which should always fail (return false).

    That is one way of thinking about solving your own problems.

    HTH

  11. #11
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    Step 1 is actually defining your problem.
    Postcodes arnt simply 6 character strings, unfortunately.
    For Cups' sake, i'm going to define the postcode in a Regex pattern. This will help him narrow down what you need to do.

    /[A-Z][A-Z]?[0-9][0-9A-Z]?\s[0-9][A-Z]{2}/

    (Hint: This regex (slightly altered) could be used to simplify your problem greatly...)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


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
  •