1

I ran into an issue with a data feed I need to import where for some reason the feed producer has decided to provide data that should clearly be either INT or FLOAT as strings-- like this:

$CASES_SOLD = "THREE";
$CASES_STOCKED = "FOUR";

Is there a way in PHP to interpret the text string as the actual integer?

EDIT: I should be more clear-- I need to have the $cases_sold etc. as an integer-- so I can then manipulate them as digits, store in database as INT, etc.

julio
  • 6,630
  • 15
  • 60
  • 82
  • does this include `"TWENTY-FOUR"` and the likes? – codingbiz Aug 28 '12 at 22:36
  • @codingbiz-- not that I've seen. It seems to be mostly 1-9 in text. – julio Aug 28 '12 at 22:38
  • If it's only a small number of values, a switch will do the trick. – Intrepidd Aug 28 '12 at 22:39
  • @Intrepidd-- I agree, but I'm hoping to do it with something akin to `intval` since otherwise I have to do a bunch of coding around for this one anomalous feed-- there's many other feeds coming in that use proper standards. – julio Aug 28 '12 at 22:41

7 Answers7

3

Use an associative array, for example:

  $map = array("ONE" => 1, "TWO" => 2, "THREE" => 3, "FOUR" => 4);
  $CASES_SOLD = $map["THREE"]; // 3
João Silva
  • 89,303
  • 29
  • 152
  • 158
1

You need a list of numbers in english and then replace to string, but, you should play with 'thousand' and 'million' clause where must check if after string 'thousend-three' and remove integer from string.

You should play with this function and try change if-else and add some functionality for good conversion:

I'm writing now a simple code for basic, but you know others what should change, play!

Look at million, thousand and string AND, it should be change if no in string like '1345'. Than replace with str_replace each of them separaterly and join them to integer.

function conv($string)
{
    $conv = array(
    'ONE' => 1,
    'TWO' => 2,
    'THREE' => 3,
    'FOUR' => 4,
    'FIVE' => 5,
    'SIX' => 6,
    'SEVEN' => 7,
    'EIGHT' => 8,
    'NINE' => 9,
    'TEN' => 10,
    'ELEVEN' => 11,
    'TWELVE' => 12,
    'THIRTEEN' => 13,
    'FOURTEEN' => 14,
    'FIFTEEN' => 15,
    'SIXTEEN' => 16,
    'SEVENTEEN' => 17,
    'EIGHTEEN' => 18,
    'NINETEEN' => 19,
    'TWENTY' => 20,
    'THIRTY' => 30,
    'FORTY' => 40,
    'FIFTY' => 50,
    'SIXTY' => 60,
    'SEVENTY' => 70,
    'EIGTHY' => 80,
    'NINETY' => 90,
    'HUNDRED' => 00,
    'AND' => '',
    'THOUSAND' => 000
    'MILLION' => 000000,
    );

    if (stristr('-', $string))
    {
        $val = explode('-', $string);

                    #hardcode some programming logic for checkers if thousands, should if trim zero or not, check if another values

        foreach ($conv as $conv_k => $conv_v)
        {
            $string[] = str_replace($conv_k, $conv_v, $string);
        }
        return join($string);
    }
    else
    {
        foreach ($conv as $conv_k => $conv_v)
        {
            $string[] = str_replace($conv_k, $conv_v, $string);
        }
        return join($string);
    }
}
Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
Marin Sagovac
  • 3,932
  • 5
  • 23
  • 53
1

If you are only interested by "converting" one to nine, you may use the following code:

$convert = array('one' => 1,
                 'two' => 2,
                 'three' => 3,
                 'four' => 4,
                 'five' => 5,
                 'six' => 6,
                 'seven' => 7,
                 'eight' => 8,
                 'nine' => 9
            );

echo $convert[strtolower($CASES_SOLD)]; // will display 3
Jocelyn
  • 11,209
  • 10
  • 43
  • 60
1

If you only need the base 10 numerals, just make a map

$numberMap = array(
    'ONE'   => 1
  , 'TWO'   => 2
  , 'THREE' => 3
  // etc..
);

$number = $numberMap[$CASES_SOLD];
// $number == 3'

If you need something more complex, like interpreting Four Thousand Two Hundred Fifty Eight into 4258 then you'll need to roll up your sleeves and look at this related question.

Community
  • 1
  • 1
Peter Bailey
  • 105,256
  • 31
  • 182
  • 206
1

Impress your fellow programmers by handling this in a totally obtuse way:

<?php

$text = 'four';

if(ereg("[[.$text.]]", "0123456789", $m)) {
    $value = (int) $m[0];
    echo $value;
}

?>
cleong
  • 7,242
  • 4
  • 31
  • 40
  • You mean, make your fellow programmers tearing one's hair. This is so weird, that i had to try it out, and indeed it works. Not that i understand how it works though... – martinstoeckli Aug 29 '12 at 14:42
0

This is similar to Converting words to numbers in PHP PHP doesn't have built in conversion functionality. You'd have to build your own logic based on switch statements or otherwise.

Or use an existing library like: http://www.phpclasses.org/package/7082-PHP-Convert-a-string-of-English-words-to-numbers.html

Community
  • 1
  • 1
Ray
  • 40,256
  • 21
  • 101
  • 138
0

Basically what you want is to write a parser for the formal grammar that represents written numbers (up to some finite upper bound). Depending on how high you need to go, the parser could be as trivial as

$numbers = ('zero', 'one', 'two', 'three');
$input = 'TWO';
$result = array_search(strtolower($input), $numbers);

...or as involved as a full-blown parser generated by a tool as ANTLR. Since you probably only need to process relatively small numbers, the most practical solution might be to manually hand-code a small parser. You can take a look here for the ready-made grammar and implement it in PHP.

Jon
  • 428,835
  • 81
  • 738
  • 806