Parse a session data with regex - php

I have a data that came from a session and I want to parse base on regex. Below are the 3 data that I want to get login, firstName and 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)
I tried to perform regexp '/\[[\/]?[A-Za-z0-9]+\]/'; but i can only get that inside the bracket.
Thank you.

Instead of parsing it using RegExp you may evaluate it.
Take a look at var_export($_SESSION['user']); . This returns a parseable string, the only problem you will see there is :
__PHP_Incomplete_Class::__set_state
"__PHP_Incomplete_Class" means, the class was unknown when the Session was started, the string still can't be parsed.
But you can take the string returned by var_export($_SESSION['user'],true) , replace the occurences of __PHP_Incomplete_Class::__set_state with array and the string can be evaluated:
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'];

Related

PHP read JSON Array with unknown variable

I have a Json Script with an Array inside and Array which looks like this shortened:
Array
(
[assets] => Array
(
[CGGD.AS] => Array
(
[shortName] => iShares Global Govt Bond Climat
[sector] =>
[industry] =>
[country] =>
[longBusinessSummary] =>
[currency] => USD
[marketCap] =>
[logo_url] =>
[Anlageklasse] => Anleihen
[Anmerkungen] => Staatsanleihen Welt
[Nachhaltigkeit] => 1
[Ist_Alternative] => 1
[weights] => 0.86563025602977
)
[SUOE.MI] => Array
(
[shortName] => ISHARES EUR CORP BOND SRI UCITS
[sector] =>
[industry] =>
[country] =>
[longBusinessSummary] =>
[currency] => EUR
[marketCap] =>
[logo_url] =>
[Anlageklasse] => Anleihen
[Anmerkungen] => Unternehmensnaleihen EUR
[Nachhaltigkeit] => 1
[Ist_Alternative] => 1
[weights] => -0.47997445382071
)
)
[risk] => 0.05323390949106
[return] => 1.1125842376311
)
Now I want to work with the single variables shortName, industry, etc.
When I tried to call the function with
print_r($json_data['assets']['CGGD.AS']['shortName']);
it worked perfectly fine. When I use
print_r($_POST['assets'][0][0]);
it is not working at all and gives me the following warning:
Warning: Undefined array key "assets" in
C:\xampp\htdocs\test\ergebnisdarstellung.php on line 52
Warning: Trying to access array offset on value of type null in
C:\xampp\htdocs\test\ergebnisdarstellung.php on line 52
The problem I have is that I will not know what name will be in the second brackets like CGGD.AS and because of this I can not use the working function. I will not now how long the array is either and numbers are not working. Because of this I do not know how to call the single variabless without using the name.
How can I call the function?
To get all shortName you can loop your array:
foreach ($json_data['assets'] as $key => $data) {
print_r($data['shortName']);
}
If you want just shortName of first element, then use current:
$item = current($json_data['assets']);
print_r($item['shortName']);
$arr = [
'assets' => [
'CGGD.AS' => [
'shortName' => 'iShares Global Govt Bond Climat',
// other fields omitted
],
'SUOE.MI' => [
'shortName' => 'ISHARES EUR CORP BOND SRI UCITS',
// other fields omitted
]
],
'risk' => 0.05323390949106,
'return' => 1.1125842376311
];
$assetsKeys = array_keys($arr['assets']);
$shortNames = array_column($arr['assets'], 'shortName');
$result = array_combine($assetsKeys, $shortNames);
print_r($result);
// This will print:
// Array
// (
// [CGGD.AS] => iShares Global Govt Bond Climat
// [SUOE.MI] => ISHARES EUR CORP BOND SRI UCITS
// )

PHP read first item in object array

I have my code in PHP which is returning this Array of data:
GoCardlessPro\Core\ListResponse Object
(
[records] => Array
(
[0] => GoCardlessPro\Resources\Mandate Object
(
[model_name:protected] => Mandate
[created_at:protected] => 2017-04-01T16:49:09.642Z
[id:protected] => ID001
[links:protected] => stdClass Object
(
[customer_bank_account] => CB001
[creditor] => CR001
[customer] => CU001
)
[metadata:protected] => stdClass Object
(
)
[next_possible_charge_date:protected] => 2017-04-06
[payments_require_approval:protected] =>
[reference:protected] => RE001
[scheme:protected] => bacs
[status:protected] => active
[data:GoCardlessPro\Resources\BaseResource:private] => stdClass Object
(
[id] => 123
[created_at] => 2017-04-01T16:49:09.642Z
[reference] => RE001
[status] => active
[scheme] => bacs
[next_possible_charge_date] => 2017-04-06
[payments_require_approval] =>
[metadata] => stdClass Object
(
)
[links] => stdClass Object
(
[customer_bank_account] => 001
[creditor] => CR001
[customer] => CU001
)
)
[api_response] =>
)
)
)
I want to be able to read the ID of the first item in therecords array.
This data is contained inside a variable called $GC_Mandate;
I have tried these:
echo $GC_Mandate->records->{0}->id;
echo $GC_Mandate->records->0->id;
echo $GC_Mandate->records->[0]->id;
$GC_Mandate = $GC_Mandate->records;
echo $GC_Mandate->{0}->id;
But none will return the data
To get the first record, the syntax you need is $GC_Mandate->records[ 0 ].
However, that object is a GoCardlessPro\Resources\Mandate object and its member id is protected1, so we'd need to know the interface of GoCardlessPro\Resources\Mandate (its public methods1), to know if we can somehow retrieve the value of id.
My guess would be getId(), so the full syntax would become
$GC_Mandate->records[ 0 ]->getId()
But, that's just a guess. You'd have to look into the documentation/class definition of GoCardlessPro\Resources\Mandate, to be sure if you can retrieve id.
Turns out (provided I'm linking to the correct github repository) you can do:
$GC_Mandate->records[ 0 ]->id
since GoCardlessPro\Resources\Mandate extends GoCardlessPro\Resources\BaseResource, which exposes the protected members through GoCardlessPro\Resources\BaseResource::__get()2.
1. visibility in PHP
2. magic methods in PHP
I can't comment so I guess I'll have to post.
You should try to print_r($GC_Mandate); and see what it gives out and then go from there.
Try $GC_Mandate->records[0]->__get('id')
it will return all data ..for perticulat data put this in foreach loop
print_r($GC_Mandate['records']);

How to display info from an array in PHP?

If my array states:
[mostPlayedGames] => Array ( [0] => stdClass Object ( [gameName] => Counter-Strike: Global Offensive [gameLink] => http://steamcommunity.com/app/730 [gameIcon] => http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/730/69f7ebe2735c366c65c0b33dae00e12dc40edbe4.jpg [gameLogo] => http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/730/d0595ff02f5c79fd19b06f4d6165c3fda2372820.jpg [gameLogoSmall] => http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/730/d0595ff02f5c79fd19b06f4d6165c3fda2372820_thumb.jpg [hoursPlayed] => 28.0 [hoursOnRecord] => 527 [statsName] => CSGO ) [1] => stdClass Object ( [gameName] => Borderlands: The Pre-Sequel [gameLink] => http://steamcommunity.com/app/261640 [gameIcon] => http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/261640/af5ef05eac8b1eb618e4f57354ac7b3e918ab1bd.jpg [gameLogo] => http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/261640/df64c72fd335a03dbcc0a19b1f81acc8db1b94ba.jpg [gameLogoSmall] => http://cdn.akamai.steamstatic.com/steamcommunity/public/images/apps/261640/df64c72fd335a03dbcc0a19b1f81acc8db1b94ba_thumb.jpg [hoursPlayed] => 10.9 [hoursOnRecord] => 10.9 [statsName] => 261640 )
and I want to display info from the first part of the array( 0 ), how would I go about doing that if I was using code like this to display it:
echo "CS:GO Hours Played: {$user->mostPlayedGames???}, PHP_EOL;
Thank you for your time.
Your mostPlayedGames array doesn't seem like it is an stdClass of any variable named $user, so let's not over complicate it.
$mostPlayedGames = [mostPlayedGames] => Array ( [0] => stdClass Object ( [gameName]....[snippet]
Now that we have that clear:
echo "CS:GO Hours Played: ${mostPlayedGames[0]->hoursPlayed}".PHP_EOL;
You see, The first element in this is an array at position 0 so we must first move to that index position. The element at index 0 is an stdClass so then we can use the accessor method -> to grab properties of this object.

Can't Access Object Variables In PHP

For some reason when I print_r a variable containing an object I get data. However when I try to access each item using the -> notation it says it is NULL.
Here is the print_r dump of $user_data:
STDCLASS OBJECT (
[UACC_ID] => 6
[UACC_GROUP_FK] => 1
[UACC_EMAIL] => xxx#GMAIL.COM
[UACC_USERNAME] => xxxx
[UACC_PASSWORD] => $2A$08$CLSxxxxxxEOS7D2NDCSDD3P6A6
[UACC_IP_ADDRESS] => xx.xxx.xx.65
[UACC_SALT] => C64JYWY7ZS
[UACC_ACTIVATION_TOKEN] =>
[UACC_FORGOTTEN_PASSWORD_TOKEN] =>
[UACC_FORGOTTEN_PASSWORD_EXPIRE] => 0000-00-00 00:00:00
[UACC_UPDATE_EMAIL_TOKEN] =>
[UACC_UPDATE_EMAIL] =>
[UACC_ACTIVE] => 1
[UACC_SUSPEND] => 0
[UACC_FAIL_LOGIN_ATTEMPTS] => 0
[UACC_FAIL_LOGIN_IP_ADDRESS] =>
[UACC_DATE_FAIL_LOGIN_BAN] => 0000-00-00 00:00:00
[UACC_DATE_LAST_LOGIN] => 2013-05-05 07:18:34
[UACC_DATE_ADDED] => 2013-04-28 09:34:39
[UGRP_ID] => 1
[UGRP_NAME] => PUBLIC
[UGRP_DESC] => PUBLIC USER : HAS NO ADMIN ACCESS RIGHTS.
[UGRP_ADMIN] => 0
[UPRO_ID] => 6
[UPRO_UACC_FK] => 6
[UPRO_COMPANY] =>
[UPRO_FIRST_NAME] => xxxx
[UPRO_LAST_NAME] => xxxxx
[UPRO_PHONE] => 01392
[UPRO_NEWSLETTER] => 0
[UADD_ID] =>
[UADD_UACC_FK] =>
[UADD_ALIAS] =>
[UADD_RECIPIENT] =>
[UADD_PHONE] =>
[UADD_COMPANY] =>
[UADD_ADDRESS_01] =>
[UADD_ADDRESS_02] =>
[UADD_CITY] =>
[UADD_COUNTY] =>
[UADD_POST_CODE] =>
[UADD_COUNTRY] =>
)
However, when I try this I get NULL:
var_dump($user_data->UACC_ID);
I can't seem to echo anything by using the notation $user_data->UACC_EMAIL etc. What am I doing wrong?
Thank you all. It appears the output was being converted to ALLCAPS in CSS and so I didn't realise that the library actually uses the lower case variants.
Thanks.
One possibilities, however strange, is that the object has a __get function defined to capture and return null on internal private data points, to prevent php from throwing fatal "cant access private" errors if you try accessing any data directly.
so, yes, UACC_ID exists, and is in there, but is being intercepted.

drupal views---how to output it

the print_r($a)'s result is
views_handle_field_node Object
(
[view]=>view Object
(
[db_table] => views_view
[base_table] => node
[args] => Array
(
[0] => My entry 1
)
[use_ajax] =>
[result] => Array
(
[0] => stdClass Object
(
[nid] => 5
[node_title] => Title of a test entry
[node_revisions_body] =>
[node_revisions_format] => 1
[node_vid] => 5
[term_data_name] => My first test term name
[term_data_vid] => 1
[term_data_tid] => 1
[vocabulary_name] => Vocabulary 1
[node_revisions_vid] => 5
)
[1]=> stdClass Object
(
[nid] => 8
[node_title] => Title of a test entry
[node_revisions_body] =>
[node_revisions_format] => 1
[node_vid] => 5
[term_data_name] => My first test term name
[term_data_vid] => 1
[term_data_tid] => 1
[vocabulary_name] => Vocabulary 1
[node_revisions_vid] => 5
..
[2]..
..
how to use one variable's name to out put the [nid]=>5 and[nid]=>8.....and all the nid.i use this, but can't work.
$views_handle_field_node->$view->$result[]->nid
The variables name is $a not `$views_handle_field_node' so try:
$first = $a->view->result[0]->nid;
//access all of the results?
foreach ($a->view->result as $obj) {
//do something with $obj->nid;
}
If that doesn't help then tell us what is the context? Are you putting this code inside a module hook or a template file? I that case what type of view settings. Tell us the "Style" and "Row style".
$first = $views_handle_field_node->view->result[0]->nid;
//access all of the results?
foreach ($views_handle_field_node->view->result as $obj) {
//do something with $obj->nid;
}

Categories