Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:


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.
  • 2
1 Solution
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)
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: http://www.perl.com/doc/manual/html/pod/perlre.html
PHP's preg_split manual: http://www.php.net/preg_split
eawebAuthor Commented:
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.
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 (www.php.net/substr).

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now