Inserting a space 2 places after a decimal in a string

In PHP, I am importing some text files containing tables of float values that are space delimited. All values contain two decimal places. A typical line would look like this:

1.45 22.87 99.12 19.55

However, some lines, if the number before the decimal is 3 digits long, the original file sometimes does not include a space. So what should be:

1.45 122.87 99.12 19.55

comes in as:

1.45122.87 99.12 19.55

What I assume I need to do is search the string for decimals, then look 2 spaces after that, and if there is not a space there I need to add one. I just cannot for the life of me figure out the most direct way to do so.

Answers


I would use regex:

    $pattern = "/(-)?\d{1,}\.\d{2}/";
    preg_match_all($pattern, "1.45122.87 99.12 19.55", $matches);
    print_r($matches);

DEMO


Need Your Help

In app purchase using parse.com SDK

ios in-app-purchase parse.com

I am utilizing the parse.com sdk for a number of things in my app, so I thought I would also piggyback on it for my in app purchase. I have an alert view with an "Upgrade" button that calls the