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

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

问题描述:

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

我的php文件 strong> p>

这是我的 用于从数据库中检索数据的代码。 p>

这里我需要根据第一个表达式(+)删除前三个字符,然后是两个数字,即(91)。 所以我需要删除数据库中包含+91的电话号码。 任何人都可以帮我解决这个问题。 p>

 &lt;?php 
 session_start(); 
 
 
 $ response = array(); 
 
 $ con = mysqli_connect(HOST,USER,  PASS,DB)或死('无法连接'); 
 
 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“成功保存”; 
 
} else {
 $ response [“error”] = true; 
 $ response [“error_msg”] =“INSERT操作失败”; 
 echo  json_encode($ response); 
} 
 //} 
} 
 
} else {
 $ response [“error”] = true; 
 $ response [“error_msg”] =“数据库连接失败 “; 
 echo json_encode($ response); 
} 
?&gt; 
  code>  pre> 
 
 

a > p> div>

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','')

Remove it using substr()

$prefix = '+91';
$str = '+912345678765';// pass your mobile number here

if (substr($str, 0, strlen($prefix)) == $prefix) {
   echo $str = substr($str, strlen($prefix));
}

DEMO

try this

$code = "+919000044440";
$number = preg_replace("/[^0-9]/","",$code);
$phone_number = substr($number,2);
echo $phone_number;

PHP str_replace() will work for you...

<?php 
$remove = '+91';
$phone_numbers = ['+919876543210','1234567890','+911234657899'];
foreach($phone_numbers as $number)
{
  echo str_replace($remove,'',$number)."
";
}
?>

This will output:

9876543210
1234567890
1234657899

LIVE EXAMPLE