PHP text-extraction from string - php

I have the following string: a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}
What I need to do is extract number 48 from it, in this case. This number could have any number of digits. How would I go about doing this?

It looks like you are facing a serialized strings. So, instead of trying to get that number using regular expression or any other string manipulation methods, try this:
$myVar = unserialize('a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}');
$myNumber = $myVar['userid'];
Learn about PHP serialization here:
http://php.net/manual/en/function.serialize.php
http://php.net/manual/en/function.unserialize.php

What exactly are you trying to achieve? That string looks like a serialize()d one, so your best bet would be to unserialize() it

It looks like serialized string.
$data = unserialize('a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}');
print_r($data['userid']);

Looks like that's a serialized associative array. You just need to use unserialize() to turn it back from a string into an array.
<?php
$arr = unserialize('a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}');
echo $arr['userid'];
?>

The string I see is serialized array in PHP
To unserialize array do this
$obj = unserialize('a:2:{s:4:"user";b:1;s:6:"userid";s:2:"48";}');
echo $obj['userid'];
I have unserialized array then access array param by name

Related

How to get a value in a multidimensional array using a string as multidimensional index?

if I have a string like this , $index_string="[1][3]";
How to get value in below array using above string,
$value_I_want_to_get_using_above_string = $multidimensional_array[$index_string];
You can do something like this using eval
$index_string="[1][3]";
$index_string =' $value_I_want_to_get_using_above_string = $multidimensional_array'.$index_string;;
eval($index_string);
Although as we all know eval() is dangerous especially if you are getting that string from another place.

Getting value from string by index

I have a string of data formatted like so:
[{"pr_a_w":"10","pr_a_we":"10","pr_c_w":"10","pr_c_we":"10"},{"pr_a_w":"20","pr_a_we":"20","pr_c_w":"20","pr_c_we":"20"},{"pr_a_w":"111","pr_a_we":"11","pr_c_w":"111","pr_c_we":"111"}]
The string doesn't have any index/numbers like a regular array would and I'm finding it difficult to extract individual values e.g. with a regular array I could use:
$string[0]["pr_a_w"]
To get the first instance of "pr_a_w" and I could use:
$string[1]["pr_a_w"]
To get the second instance etc.
Is it possible to get single values from this string based on their number?
What you have there is valid JSON (serialized array of objects), so you could use json_decode to translate the serialized data into a native PHP array:
$array = json_decode('[{"pr_a_w":"10","pr_a_we":"10","pr_c_w":"10","pr_c_we":"10"},{"pr_a_w":"20","pr_a_we":"20","pr_c_w":"20","pr_c_we":"20"},{"pr_a_w":"111","pr_a_we":"11","pr_c_w":"111","pr_c_we":"111"}]',true);
$array will then allow you to do exactly what you stated you'd like to do above.
$array[0]["pr_a_w"]; // will give you 10
$array[1]["pr_a_w"]; // will give you 10
Try like this, No need to access with array index. You will get error if you access wrong index.
$json_arr= json_decode('[{"pr_a_w":"10","pr_a_we":"10","pr_c_w":"10","pr_c_we":"10"},{"pr_a_w":"20","pr_a_we":"20","pr_c_w":"20","pr_c_we":"20"},{"pr_a_w":"111","pr_a_we":"11","pr_c_w":"111","pr_c_we":"111"}]',true);
foreach($json_arr as $row){
echo $row['pr_a_w']."<br>";
}

cast string to JSON

Is there any way to make PHP look at a string as a JSON?
I have a string, in a JSON format of course, and I want to perform actions on it like it was an array. However I don't want to use CJSON::decode because it takes a long time, Is there a way?
Example for the string:
{"myArray":[{"key1":"val1","key2":"val2","key3":"val3","key4":"val4"},
{"key1":"val2_1","key2":"val2_2","key3":"val2_3","key4":"val2_4"}]}
Not sure what CJSON::decode is but you have to decode the string so you can use the built in function json_decode($str);
how about json_decode() ?
I don't think there is faster than that
$string = '{"myArray":[{"key1":"val1","key2":"val2","key3":"val3","key4":"val4"}, {"key1":"val2_1","key2":"val2_2","key3":"val2_3","key4":"val2_4"}]}';
$array = json_decode($string);

Coverting String to PHP Associative Array

How can i convert this string
$str = "array('3'=>'wwm','1'=>'wom')";
to real php associative array...
It's simple but REALLY INSECURE
$str = "array('3'=>'www.tension.com','1'=>'www.seomeuo.com','requestedBy'=>'1')";
eval("\$array = $str;");
You never should use this approach, there another ways to do it like: serialize() and unserialize()
You can use the eval() function for that:
$str = "array('3'=>'wwm','1'=>'wom')";
eval("\$a=$str;");
var_dump($a);
However using eval() in your code is considered to be risky and you should not use it. Try to use serialize(), unserialize() instead.
First of all. Do not use eval. It is Evil!
http://af-design.com/blog/2010/10/20/phps-eval-is-evil/
Secondly. The simple solution would not to be using this string but simply to use "serialize" when you put it in the DB and unserialize when you pull it out. You are storing a very unusual format.

Parsing serialized Array (Wordpress)

I'm using wordpress and one of my meta key values is stored like this:
a:1:{i:0;s:8:"Religion";}
I'm trying to figure out the easiest way in PHP to parse this so I can extract "Religion" or really any of the elements in a clean manner.
Hope this makes sense - thanks!!!
Loren
that is a serialized array, use unserialize()
$array = unserialize('a:1:{i:0;s:8:"Religion";}');
echo $array[0];
This works in PHP 5.2 and above:
list($thatWord) = unserialize($metaKeyValue);
You then have the string "Religion" in $thatWord.
The string you have:
a:1:{i:0;s:8:"Religion";}
Is a serialized array. If you retrieve it from within wordpress, you would get an Array instead of a string. I assume you pull it from the database directly, so you need to unserializeDocs it your own.

Categories