Split String
Results 1 to 3 of 3

Thread: Split String

  1. #1
    Senior Member
    Join Date
    Oct 2005
    Posts
    115

    Split String

    Hi All
    Is there a way i can split the items in a string into seperate vars .. $string= "> Cat 1 > subcat 1 > subcat 2" into

    $var1="Cat 1"
    $var2="subcat 1"
    $var3="subcat 2"


    thanks for any help

  2. #2
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,811
    You could consider using explode. Something like this:
    PHP Code:
    $string"> Cat 1 > subcat 1 > subcat 2";
    $my_array explode(">"$string); 
    Note that this might result in $my_array having a values with just spaces in them and the other slots having values with a space before and/or after. You'd need to loop through the array and clean it up a bit.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  3. #3
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,820
    If you want to get rid of any excess white space, but cannot always be sure if it will be there, preg_split() can be useful, too:
    PHP Code:
    $parts preg_split('/\s*>\s*/'$stringnullPREG_SPLIT_NO_EMPTY);
    // last arg so leading ">" does not create empty element 
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because hes a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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