dsvbtgo639708 2014-11-05 20:39 采纳率: 0%
浏览 58
已采纳

Php将字符串转换为正确的类型

I really looking for one function that can help me convert value like this (after var_dump):

string(2) "49" to integer or string(3) "1.2" to float.

this doesn't work for me Thanks

Here is my code: I simply have one Recursion function to check all elements of my array

            foreach($arr as $key => $value)
            {
                if(is_array($value))
                {
                    $this->__set_types($value);
                }
                if(is_numeric($value))
                {
                    echo getcorrectvariable($value);
                    $arr[$key] = settype($value, gettype_fromstring($value));
                    var_dump($arr);
                }
            }   

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 (https://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;
}

Here is one pice of my array (var_dump)

array(3) {
  [0]=>
  array(3) {
    [0]=>
    array(9) {
      ["id"]=>
      string(3) "106"
      ["idMessageType"]=>
      string(1) "1"
      ["idClientSender"]=>
      string(2) "59"
      ["idClientRecipient"]=>
      string(2) "49"
      ["message"]=>
      string(5) "rwert"
      ["creationTimeStamp"]=>
      string(22) "2014-11-04 10:09:12+05"
      ["readingTimestamp"]=>
      NULL
      ["status"]=>
      string(1) "1"
      ["isMessageActive"]=>
      string(1) "t"
    }
    [1]=>
    array(9) {
      ["id"]=>
      string(3) "105"
      ["idMessageType"]=>
      string(1) "1"
      ["idClientSender"]=>
      string(2) "59"
      ["idClientRecipient"]=>
      string(2) "49"
      ["message"]=>
      string(7) "qwrqewr"
      ["creationTimeStamp"]=>
      string(22) "2014-11-04 10:08:56+05"
      ["readingTimestamp"]=>
      NULL
      ["status"]=>
      string(1) "1"
      ["isMessageActive"]=>
      string(1) "t"
    }
    ["client"]=>
    array(4) {
      ["id"]=>
      string(2) "49"
      ["name"]=>
      string(12) "а аАаВаИаЛб"
      ["surname"]=>
      string(14) "ааАбаИаПаОаВ"
      ["photoName"]=>
      string(50) "49.jpg                                            "
    }
  }
  • 写回答

2条回答 默认 最新

  • doujiao1814 2014-11-05 20:50
    关注

    floatval or intval will do what you ask, separately.

    If you want to retrieve the type... doesn´t make sense to me, but you could make a custom function using a regex... here:

    function getTypedNumber($string)
    {
    $pattern = '/^(\d{1,3})((,)(\d{3}))*((\.)(\d{1,2}))?$|^(\d{1,3})((\.)(\d{3}))*((,)(\d{1,2}))?$/';
    $replacement = '\1\8\4\11.\7\14';
    $number = preg_replace($pattern, $replacement,$string);
    if (is_numeric($number){
     if (strpos($number, ".") === false )  return intval($number);
     else return floatval($number);
    }
    else return false;
    }
    

    It will process number in the format 10.300,25 or 10,300.25 as well

    EDIT

    Forgot, that regex works assuming 2 decimal positions, it can be changed easily, if needed, but then it won´t process French/English Number style

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助