douben8492 2016-06-22 11:41
浏览 122
已采纳

如何在插入数据库之前根据第一个字母(表达式)修剪前三个字符

my php file

This is my code to retrieve data from database.

Here i need to remove first three characters based on the first expression(+) and followed by two numbers i.e(91). So totally i need to remove phone numbers which have +91 in database. Can anyone help me regarding this.

    <?php
    session_start();


    $response = array();

    $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');

    if(!mysqli_connect_errno()){

        $error_flag = false;

    $contacts = json_decode($_POST['contacts'], true);
    foreach($contacts as $contact){

            //$trimmed = $contact['phone'];

            //$title = str_replace("+91", "", trim($trimmed));
            // $prefix = '+91';
            // $str = $contact['phone'];
            // if (substr($str, 0, strlen($prefix)) == $prefix) 
            //     { echo $str = substr($str, strlen($prefix)); }


            $sql = "INSERT INTO contacts (vault_no , name, phone, created_at)
            VALUES ('".$contact['vault_no']."', '".$contact['name']."', REPLACE('".$contact['phone']."','+91',''), NOW())";

                if(mysqli_query($con,$sql)){

                    echo "Successfully Saved";

                }else{
                    $response["error"] = true;
                    $response["error_msg"] = "INSERT operation failed";
                    echo json_encode($response);
                }
                    //}
    }

    }else{
        $response["error"] = true;
        $response["error_msg"] = "Database connection failed";
        echo json_encode($response);
    }
?>

this is my contact list in database

  • 写回答

4条回答 默认 最新

  • doutangdan3588 2016-06-22 11:43
    关注

    You can simply use REPLACE() :

    SELECT REPLACE(t.mobile,'+91','') as mobile
    FROM YourTable t
    

    Or if you want to change it in the database :

    UPDATE YourTable t
    SET t.mobile = REPLACE(t.mobile,'+91','')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA