Solved

How to extract a string within another ?

Posted on 2004-11-01
179 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
Question by:Richard Coffre
    7 Comments
     
    LVL 32

    Expert Comment

    by:ldbkutty
    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
    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
    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
     
    LVL 48

    Accepted Solution

    by:
    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
    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
    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
    Thanks for all your answers.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Cisco Complete Network Certification Training

    If you’re an IT engineer or technician, it's time you take your career to the next level. This elite training bundle is brimming with all of the information you need to learn to sit for Cisco CNNA, CCNP, and CCENT certification exams.

    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    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…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to dynamically set the form action using jQuery.

    934 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now