Posted on 2006-06-02
Last Modified: 2012-06-21
i have this data "241161 20041022C 1675.27Geernaert-Clemencia, M" in an array which i'am splitting from the first 'C'
with this code.

  echo $splitDDA[1];
  echo "</br>";
  echo $splitAfterDDATillC[0];
  echo"data after DDA till C";
  echo "</br>";
  echo $splitAfterDDATillC[1];

and it works.  
here the results:

  241161 20041022C 1675.27Geernaert-Clemencia, M data after DDA
  241161 20041022data after DDA till C
  1675.27Geernaert-Clemencia, M data after DDA after C
  but now i'am trying to split the first numeric data 241161 20041022 of $splitDDA[1] and
  display "C 1675.27Geernaert-Clemencia, M"
  how must i do that because i tried $splitAfterDDATillC=split('[digit]',$splitDDA[1]);
  but nothing i didn't succeed.
  please advice with some code.
Question by:eaweb
    LVL 5

    Expert Comment

    You will have to use php's regular expression split to do that

    If you dont know any regular expression then put another post and I can give you the code (its one line, easy to build if you have done regular expression before)
    LVL 8

    Expert Comment

    Something like this should do the trick:


    # This is your string stored in $splitDDA[1]
    $stringOne = "241161 20041022C 1675.27Geernaert-Clemencia, M";
    # This says to split the string wherever it finds 1 or more digits followed by 1 or more spaces and another 1 or more digits...
    $splitArray = preg_split("/[\d]+[\s]+[\d]+/", $stringOne);
    # So then the string you want is in $splitArray[1]
    echo "This is the string after the beginning rows of digits: " . $splitArray[1];


    Another example to clarify:

    $stringOne = "hello32432there234324my234324friend2323523";
    $splitArray = preg_split("/[\d]+/", $stringOne);
    foreach($splitArray as $item)
         echo "$item<br>";

    Gives you:


    There are a lot of other things you can do with preg (meaning perl regular expressions).  If you look at a tutorial on perl regular expressions you will see all of the other options you can use with php's "preg" function (the syntax of the regular expressions should be the same, but you just need to put them in the preg_split PHP function).

    Perl regex tutorial:
    PHP's preg_split manual:

    Author Comment

    thank you very very much. your help was wonderfull.
    but now i have this problem with with this long string of numbers

      sometimes the data comes like this
      [   936    230012.10   936    230012.10     0         0.00]
      and sometimes like this
      what i always need is the fisrt 6 characters with or without 0 or space like this [000936] or [   936] beacause as you see the positions remain the same for both.
      and i will need the data too after the fisrt 6 charater till the two numebrs after the first point[0000230012.10] or
      [    230012.10] of the string
      how can i do that with the same procedure of split or must i do that i another way.
    LVL 8

    Accepted Solution

    There may be a way to do this again with regular expressions, but it seems like that is overkill in this case, especially because all you want to do is break up the string on specific character # boundaries (i.e. take the first 6 characters, then take the next 13 characters, then take the next 6 characters, then the last 13 characters).  You asked to get the string up until 2 characters after the decimal, but in the example you posted this always equals 13 characters, so I am assuming that is the case.  If this is not the case, let me know.

    To do that you can do this (using the substr method in php):

            # This function breaks up the string by the character boundaries you defined (6/13/6/13)
            function breakUpString($stringToBreak)
                    $returnString = "";
                    $startIndex = 0;
                    $lenStr = strlen($stringToBreak);
                    while($startIndex != $lenStr)
                            # Grab 6 characters (spaces, zeros, and other digits)
                            $returnString .= "=====" . substr($stringToBreak, $startIndex, 6) . "=====<br>";
                            $startIndex += 6;
                            # Grab next 13 chars (from after the first 6 digits and until 2 digits after the decimal)
                            $returnString .= "=====" . substr($stringToBreak, $startIndex, 13) . "=====<br>";
                            $startIndex += 13;
                    return $returnString;

            # The strings of the two different formats
            $stringFormatOne = "   936    230012.10   936    230012.10     0         0.00";
            $stringFormatTwo = "0009360000230012.100009360000230012.100000000000000000.00";

            # Now break up the two strings
            echo breakUpString($stringFormatOne) . "<br>";
            echo breakUpString($stringFormatTwo) . "<br>";

            # You can also replace the " " characters with "0"s if you want (or the other way around if you want)
            echo str_replace(" ", "0", breakUpString($stringFormatOne)) . "<br>";

    The output:

    #(This is the first string broken up)
    ===== 936=====
    ===== 230012.10=====
    ===== 936=====
    ===== 230012.10=====
    ===== 0=====
    ===== 0.00=====

    #(This is the second string broken up)

    #(This is the first string broken up again, but with the spaces replaced by 0s)

    Look up the php substr documentation to see how I used substr (

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Building “do-it-yourself” web sites has become an epidemic. There are so many blogs, web sites and even books that "teach" you how to build your web site in a few extremely simple and easy steps. Building a web site has become easier than boiling an…
    I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
    Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    734 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

    18 Experts available now in Live!

    Get 1:1 Help Now