dougehe2022 2019-01-22 08:11
浏览 109
已采纳

如何使用多个正负符号将字符串转换为int?

I'm trying to cast string to int. Everything work fine but have a small problem here

echo (int)"--12"; //return 0, i want 12
echo (int)"---23";//return 0, i want -23
echo (int)"+-99"; //return 0, i want -99
...

Why did this happened and what is the right way to cast in this case? Many thanks!

  • 写回答

3条回答 默认 最新

  • douqin7086 2019-01-22 08:37
    关注

    To evaluate the positive and negative signs so that all positive symbols are ignored and two negatives equal a positive, you can perform a replacement.

    Code: (Demo)

    $signed_number_strings = ["--1", "---33", "+-444"];
    
    foreach ($signed_number_strings as $string) {
        var_dump((int)preg_replace('~\++|-\+*-\+*~', '', $string));
    }
    

    Output:

    int(1)
    int(-33)
    int(-444)
    

    The logic behind the pattern is to first match/remove 1 or more consecutive + signs, OR a - sign followed by zero or more + followed by a - (and absorbing any trailing + signs). If there are any fringe cases that my pattern doesn't correctly handle, please update your question and leave me a comment.

    p.s. The extension of my 2nd branch with \+* is an attempt to optimize the pattern so that it doesn't have to restart the pattern. It could have been written as ~\++|-\+*-~ which would be slightly less strain on the eyeballs. (Demo)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • douweilaton2762 2019-01-22 08:24
    关注

    you can extract number from string . can help?!

    $str = '--1';
    preg_match_all('!\d+!', $str, $matches);
    print_r($matches);
    
    评论
  • dongzizhi9903 2019-01-22 08:33
    关注

    I am not 100% sure on your use case but I knocked up this function to try and handle a string with numbers and to return the first full number with negative signed support.

    I am sure I have your use case wrong but you can butcher this function however you like to drop the negative signed handling or to possibly handle floats where a decimal place may be needed for detection.

    Worth noting that a regex may even exist for this.. who knows.

    function intvaljunk($string) {
        $boolSignedNegative = false;
        $intIntStart = null;
        $intIntEnd = null;
        foreach (str_split($string) as $index => $char) {
            if (is_numeric($char)) {
                $boolSignedNegative = (
                    $boolSignedNegative === false
                    && $index > 0
                    && is_null($intIntStart)
                    && $string[$index - 1] === '-'
                        ? true
                        : $boolSignedNegative
                );
                $intIntStart = is_null($intIntStart) ? $index : $intIntStart;
                $intIntEnd = is_null($intIntStart) ? $intIntEnd : $index;
            } else if (!is_null($intIntStart) && !is_null($intIntStart)) {
                break;
            }
        }
    
        return (
            !is_null($intIntStart) && !is_null($intIntStart)
            ? intval(($boolSignedNegative ? '-' : '') . substr($string, $intIntStart, ($intIntEnd - $intIntStart) + 1))
            : null
        );
    }
    
    echo intvaljunk("--1") . PHP_EOL;
    echo intvaljunk("---1") . PHP_EOL;
    echo intvaljunk("+-1") . PHP_EOL;
    echo intvaljunk("+-14991abc667") . PHP_EOL;
    
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求苹果推信imessage批量推信技术
  • ¥15 ubuntu 22.04 系统盘空间不足。隐藏的docker空间占用?(相关搜索:移动硬盘|管理系统)
  • ¥15 利用加权最小二乘法求亚马逊各类商品的价格指标?怎么求?
  • ¥15 c++ word自动化,为什么可用接口是空的?
  • ¥15 Matlab计算100000*100000的矩阵运算问题:
  • ¥50 VB6.0如何识别粘连的不规则的数字图片验证码
  • ¥16 需要完整的这份订单所有的代码,可以加钱
  • ¥15 Stata数据分析请教
  • ¥15 请教如何为VS2022搭建 Debug|win32的openCV环境?
  • ¥15 关于#c++#的问题:c++如何使用websocketpp实现websocket接口调用,求示例代码和相关资料