Split String
Results 1 to 3 of 3

Thread: Split String

  1. #1
    Senior Member
    Join Date
    Oct 2005

    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
    Flanders Fields
    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
    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 
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

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