使用regex解析会话数据

时间:2012-10-29 01:27:57

标签: php regex

我有一个来自会话的数据,我想基于正则表达式解析。下面是我想要登录的3个数据,firstName和lastName。

login = James 
firstName = James.S 
lastName = Steal


Array (
[user] => __PHP_Incomplete_Class Object (
    [__PHP_Incomplete_Class_Name] => DEX_User
    [permissionID:DE_User:private] => 9
    [login:DE_User:private] => James.S
    [email:DE_User:private] => james.s@domain.com
    [firstName:DE_User:private] => James
    [lastName:DE_User:private] => Steal
    [title:DE_User:private] => Warehouse Man
    [manager:DE_User:private] => Manager's Name
    [workPhone:DE_User:private] => +1 (111) 111-1111
    [mobilePhone:DE_User:private] => +1 (222) 222-2222
    [homePhone:DE_User:private] => +1 (333) 333-3333
    [im:DE_User:private] =>
    [timeDelta:DE_User:private] => Asia/Hongkong
    [lastLogin:DE_User:private] => __PHP_Incomplete_Class Object ( 
        [__PHP_Incomplete_Class_Name] => GB_Date
        [valueStored:protected] => 13545544126666309821
        [mode:protected] => BASE
        [master:protected] =>
    )
    [description:DE_User:private] => Warehouse Engineer
    [isActive:DE_User:private] => __PHP_Incomplete_Class Object (
        [__PHP_Incomplete_Class_Name] => GB_Boolean
        [valueStored:protected] => 1
        [mode:protected] => BASE
        [master:protected] =>
    )
    [isTerminate:DE_User:private] => __PHP_Incomplete_Class Object (
        [__PHP_Incomplete_Class_Name] => GB_Boolean
        [valueStored:protected] =>
        [mode:protected] => BASE
        [master:protected] =>
    )
    [id:protected] => 231968
    [isModifyed:protected] => 
    [needInsert:protected] => 
    [isDeleted] => 
    [isRemoved] => 
)
[enter_password] => 2asas(qwqw)

我尝试执行regexp '/\[[\/]?[A-Za-z0-9]+\]/';,但我只能在括号内进行。

谢谢。

1 个答案:

答案 0 :(得分:4)

您可以使用RegExp对其进行分析,而不是使用RegExp对其进行分析。

看看var_export($_SESSION['user']);。这将返回一个可解析的字符串,您将看到的唯一问题是:

__PHP_Incomplete_Class::__set_state

“__ PHP_Incomplete_Class”表示,启动Session时类未知,仍无法解析字符串。

但您可以使用var_export($_SESSION['user'],true)返回的字符串,将__PHP_Incomplete_Class::__set_state的出现替换为array,并且可以评估该字符串:

eval('$user='.str_replace('__PHP_Incomplete_Class::__set_state',
                          'array',
                          var_export($_SESSION['user'],true)).';');

echo $user[0]['login'].',<br/>'.
     $user[0]['firstName'].',<br/>'.
     $user[0]['lastName'];