1

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?

0

5 Answers 5

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

It looks like serialized string.

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

Comments

0

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'];
?>

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.