Beginner Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
funngyal4
Forum Newbie
Posts: 1
Joined: Thu Aug 19, 2010 10:21 am

Beginner Help

Post 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 ?
kurbot
Forum Newbie
Posts: 18
Joined: Tue Jul 20, 2010 2:20 pm

Re: Beginner Help

Post 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"
        )

)

User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Beginner Help

Post 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);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply