Page 1 of 1

Beginner Help

Posted: Thu Aug 19, 2010 10:29 am
by funngyal4
Hi folks -
I have a question with string manipulation. I'm new to PHP, hence any help you provide will be appreciated.

I have a string that looks like this:
$str = 'active|b:1;loans|i:2;name|s:12:"Abcd Efgh";email|s:17:"abcd@efgh.com";ip|s:14:"111.110.11.111"';

I want to extract each value separately.. for example:
name= "Abcd Efgh"
email="abcd@efgh.com"

I started using explode, but I'm sure there is a better function to split this string. Any ideas ?

Re: Beginner Help

Posted: Thu Aug 19, 2010 11:18 am
by kurbot

Code: Select all

$str = 'active|b:1;loans|i:2;name|s:12:"Abcd Efgh";email|s:17:"abcd@efgh.com";ip|s:14:"111.110.11.111"';

$rows 				= array();
$data 				= array();
$data2 				= array();
$rows 				= explode(';',$str);

$i=0;
foreach($rows as $value){

   $data			= explode('|',$value);	
   $data2 			= explode(':',$data[1]);	
	
   $arrData[$i]['label']	= $data[0];
   $arrData[$i]['type']		=$data2[0];
   $arrData[$i]['length']	=$data2[1];
   $arrData[$i]['txt']		=$data2[2];

$i++;
}
echo '<pre>';
print_r($arrData);
echo '</pre>';

// Prints out this
Array
(
    [0] => Array
        (
            [label] => active
            [type] => b
            [length] => 1
            [txt] => 
        )

    [1] => Array
        (
            [label] => loans
            [type] => i
            [length] => 2
            [txt] => 
        )

    [2] => Array
        (
            [label] => name
            [type] => s
            [length] => 12
            [txt] => "Abcd Efgh"
        )

    [3] => Array
        (
            [label] => email
            [type] => s
            [length] => 17
            [txt] => "abcd@efgh.com"
        )

    [4] => Array
        (
            [label] => ip
            [type] => s
            [length] => 14
            [txt] => "111.110.11.111"
        )

)


Re: Beginner Help

Posted: Thu Aug 19, 2010 1:23 pm
by AbraCadaver
That's actually session encoded data (what you posted isn't valid because you changed the name/email/ip/etc.). Where did you get it/why does it need to be in that format? This will restore it back to the session so you can use it:

Code: Select all

session_start();
session_decode($str);
print_r($_SESSION);