[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to extract a string within another ?

Posted on 2004-11-01
7
Medium Priority
?
192 Views
Last Modified: 2008-02-20
Hi all,

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

s:14:\"PaymethodTotal\";s:1:\"0\";s:5:\"Total\";d:62;s:6:\"Remark\";s:0:\"\";

How can I do that ?

TIA
0
Comment
Question by:Richard Coffre
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 32

Expert Comment

by:ldbkutty
ID: 12464791
You can use explode() function to extract the string:

<?php
     $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.
0
 
LVL 1

Author Comment

by:Richard Coffre
ID: 12464884
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 ?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 12464958
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.

Regards

-r-
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Accepted Solution

by:
hernst42 earned 2000 total points
ID: 12464966
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];
}

0
 
LVL 10

Expert Comment

by:eeBlueShadow
ID: 12466160
try:

$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)) {
    print_r($m);
}

after the preg_match_all, $m[1] should have a list of all the 'd' values
0
 
LVL 2

Expert Comment

by:suresh_asp
ID: 12469959
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];
0
 
LVL 1

Author Comment

by:Richard Coffre
ID: 12601626
Thanks for all your answers.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question