views:

55

answers:

2

I was searching here on StackOverflow about converting string to the real value and i didn't found. I need a function like "gettype" that does something like the result above, but i can't do it all :s

gettypefromstring("1.234"); //returns (doble)1,234;
gettypefromstring("1234"); //returns (int)1234;
gettypefromstring("a"); //returns (char)a;
gettypefromstring("true"); //returns (bool)true;
gettypefromstring("khtdf"); //returns (string)"khtdf";

Thanks to all :)

+5  A: 

You must try to convert it in specified order:

  1. Check is double
  2. If double, this may be a integer, you must convert and compare this values.
  3. If not, this is char if lenght is == 1.
  4. If not, this is string.
  5. If string, try parse to bool.

You can't use gettype because you may get string type of decimal writed in string.

Svisstack
Thanks, i will try that way. Yes, i know, thats why i want this function ;)
CuSS
Problem, and if the string has "12ab3"? it is string right? how can i know if the string has alphanumeric chars?
CuSS
"12ab3" is not int, because have a alphanumeric, then algorithm not check double from it, they lenght is bigger than 1, then this is string, and cant parse it to bool. Result is string, and real result is string because "12ab3" is not a valid number.
Svisstack
i have added the function here as answer and with credits to you, thanks ;)
CuSS
+1  A: 

Hi, 1+ for Svisstack! ;)

Here is the function if someone want it:

function gettype_fromstring($string){
    //  (c) José Moreira - Microdual (www.microdual.com)
    return gettype(getcorrectvariable($string));
}
function getcorrectvariable($string){
    //  (c) José Moreira - Microdual (www.microdual.com)
    //      With the help of Svisstack (http://stackoverflow.com/users/283564/svisstack)

    /* FUNCTION FLOW */
    // *1. Remove unused spaces
    // *2. Check if it is empty, if yes, return blank string
    // *3. Check if it is numeric
    // *4. If numeric, this may be a integer or double, must compare this values.
    // *5. If string, try parse to bool.
    // *6. If not, this is string.

    $string=trim($string);
    if(empty($string)) return "";
    if(!preg_match("/[^0-9.]+/",$string)){
        if(preg_match("/[.]+/",$string)){
            return (double)$string;
        }else{
            return (int)$string;
        }
    }
    if($string=="true") return true;
    if($string=="false") return false;
    return (string)$string;
}

I used this function to know if the number X is multiple of Y.

Example:

$number=6;
$multipleof=2;
if(gettype($number/$multipleof)=="integer") echo "The number ".$number." is multiple of ".$multipleoff.".";

But the framework that i work returns always the input vars as strings.

CuSS
You have some extra code. `empty` already tests for an empty string. Why are you using `|| $striing == ""`? Also, you're casting a string to a string. PHP knows that `""` is a string. That line could be rewritten as: `if (empty($string)) return ""`. Also, it doesn't make sense to do `return (bool)true/false`. `true` and `false` are already booleans; they don't need to be casted.
ryeguy
thanks ryeguy ;)
CuSS