How to extract a string within another ?

Hi all,

I would like to extract the amount "62" in the following string. It corresponds to the total amount.


How can I do that ?

Richard Coffree-commerce Product ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can use explode() function to extract the string:

     $myString = 's:14:\"PaymethodTotal\";s:1:\"0\";s:5:\"Total\";d:62;s:6:\"Remark\";s:0:\"\"';
     $getSubString = explode(';', $myString);
     echo $getSubString[3];   // Displays d:62
     $getNumber = explode(':', $getSubString[3]);
     echo "<br> " . $getNumber[1]; // Diplays 62

PS: There are numerous other ways like strpos(), strrchr(). It depends on how your string is organized.
Richard Coffree-commerce Product ManagerAuthor Commented:
Is there a way to define a pattern,  e.g. s:5:\"Total\";d:$$;s:6:\"Remark\" where $$ represents the value we want to retrieve ?
The looks a awfull lot like a string being serialized with serialize.

Possibly you can use:

$array = unserialize('....text u showed....');

and then $array['Total'] holds the value 62.


Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

you can try using a regular expression like this:

$str = 's:14:\"PaymethodTotal\";s:1:\"0\";s:5:\"Total\";d:62;s:6:\"Remark\";s:0:\"\"';
if (preg_match('/Total.?";d:(\d+(\.\d+)?)/', $str, $m)) {
    echo $m[1];


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

$str = 's:14:\"PaymethodTotal\";s:1:\"0\";s:5:\"Total\";d:62;s:6:\"Remark\";s:0:\"\"';
$pattern = ';d:(\d+);';

if (preg_match_all($pattern, $str, $m, PREG_SET_ORDER)) {

after the preg_match_all, $m[1] should have a list of all the 'd' values
Try this,

$string = "s:14:\"PaymethodTotal\";s:1:\"0\";s:5:\"Total\";d:62;s:6:\"Remark\";s:0:\"\";";
$changed = preg_match_all("/d:(\d+)/",$string,$res);
echo $res[1][0];
Richard Coffree-commerce Product ManagerAuthor Commented:
Thanks for all your answers.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.