Advanced Search

Results 1 to 2 of 2

Thread: String to Array function

  1. #1
    Join Date
    May 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default String to Array function

    Does anybody know what function will turn a string into an array where key & value are contained in the string?

    example:

    $string = "LED: no LED\nCustom Text: \nText Position: Bottom Centre\nColor: Blue"

    into array:

    [LED] => no LED
    [Custom Text] =>
    [Text Position] => Bottom Centre
    [Color] => Blue


    Regards

    Hebs

  2. #2
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    Code:
    <?php
    function splitc($str,$d) {
    preg_match_all("|([a-zA-Z0-9]+):[ ](.*)[".$d."]?|U",$str,$out, PREG_SET_ORDER);
    for($i=0;$i<count($out);$i++) {
    $arr[$out[$i][1]]=$out[$i][2];
    }
    return $arr;
    }
    to use:
    Code:
    $codes=splitc("code1: five;code2: 7;", ";");
    or
    Code:
    $string=splitc("LED: no LED\nCustom Text: \nText Position: Bottom Centre\nColor: Blue", "\n");
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@ /\/\@|{
    There are 10 kinds of people in the world, those that understand binary and those that don't.

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
  •