SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: String Parse?

  1. #1
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    String Parse?

    Hey,

    I have a registration page where i am inserting a value called "constituency".

    Users have the option from the followiing:-

    Bury North - David Chaytor, Labour
    Bury South - Ivan Lewis, Labour
    Cheadle - Mark Hunter, Liberal Democrat
    Now when users are logged in i want to be able to display this. BUT i want to separate some of the words..

    How can i take for example this value from the database:-

    Cheadle - Mark Hunter, Liberal Democrat
    And display it like this:-

    This is your local constituency office for Cheadle
    Your MP is Mark Hunter, Liberal Democrat
    I have underlined the values from the dababase

    Regards
    Billy

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    If the two parts are always separated by a '-', then you can use explode

    Or you might change your database structure

  3. #3
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    With simple string manipulation tasks like this, there are usually a number of different ways of achieving the desired result. One such approach would be to simply split the two parts of the string into their component pieces. As it happens, they are separated by space-hyphen-space so we can make use of the explode function (and in this case, list also):

    PHP Code:
    $constituency "Cheadle - Mark Hunter, Liberal Democrat";
    list(
    $office$mp) = explode(" - "$constituency2);

    echo 
    "
    This is your local constituency office for 
    $office
    Your MP is 
    $mp
    "

    Salathe
    Software Developer and PHP Manual Author.

  4. #4
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should probably store them in separate columns in the database.

    Anyway, you can do this:

    PHP Code:
    $data "Cheadle - Mark Hunter, Liberal Democrat";

    list(
    $region$name) = explode(" - "$data); 

  5. #5
    SitePoint Addict
    Join Date
    Jan 2002
    Location
    Southwest Florida
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm familiar with the list construct, but I'm not sure what it would do if there's more than one hyphen in the data string -- e.g., "Cheadle - Mark Wiggins-Hunter, Liberal Democrat."

    Given how common it is for personal names to contain hyphens, and it's not unheard of for place names to contain hyphens, listing and exploding isn't an ideal solution.

    I'd recommend that the database format be changed to at least 3 columns: name, location, and political label. Using separate columns for first and last name would be even better, because that would provide the flexibility to list names alphabetically by last name or alphabetically by first name, or to list them as last name, first name.

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sonjay View Post
    I'm familiar with the list construct, but I'm not sure what it would do if there's more than one hyphen in the data string -- e.g., "Cheadle - Mark Wiggins-Hunter, Liberal Democrat."

    Given how common it is for personal names to contain hyphens, and it's not unheard of for place names to contain hyphens, listing and exploding isn't an ideal solution.
    Given what was presented in the first post, the simple explode might well be all that is necessary. Bear in mind that the string is exploded (at least in mine and decowski's code snippets) by the value space, hyphen, space rather than just a hyphen: this completely bypasses your worries about hyphenated names though doesn't cater for strings formatted with two or more space, hyphen, space sequences (that said, the first post makes no mention of that being a possibility). Also, in my example the explode is given a value (2) for the limit parameter (3rd) which means that only the first space, hyphen, space (if there is one or more) will used to split the string; any further instances of that sequence will be ignored and simply returned as the second array value ($mp in my list statement). Again the OP makes no reference as to whether this might happen or not: we can only work with what was presented.
    Salathe
    Software Developer and PHP Manual Author.

  7. #7
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Guys,

    I'm using the explode method for now and it works. I will eventually spearate the columns because it seems the best way to go!

    Thanks
    Regards

  8. #8
    SitePoint Addict
    Join Date
    Jan 2002
    Location
    Southwest Florida
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    Bear in mind that the string is exploded (at least in mine and decowski's code snippets) by the value space, hyphen, space
    Oh, I didn't even catch that! Yes, that should address the concern I expressed about hyphenated names and place names.

  9. #9
    SitePoint Enthusiast nrg_alpha's Avatar
    Join Date
    Dec 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Having multiple values within a database field is simply bad form, as it violates 1st normal form (which requires fields to contain only one value (a.k.a being atomic)).

    This is why it is important to learn about stuff like the E-R model, form normalization and the like when learning about databases, otherwise you run into problems like this.


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
  •