使用php同步电话簿

时间:2017-04-14 06:25:25

标签: php mysql regex yii2

我正在开发使用yii2框架同步联系人的网络服务。我从移动端拥有整个电话簿(在json数组中),我必须检查我的数据库中有哪些电话号码。

请求参数就像这样

{
    "user_id": "4",
    "user_details": [{
        "first_name": "A",
        "last_name": "A"

    }, {
        "first_name": "B",
        "last_name": "B"
        ,
        "mobile_number": ["(888) 888-888", "(777) 777-777"]
    }, {
        "first_name": "C",
        "last_name": "C",
        "mobile_number": ["+918000584123", "(666) 666-6666", "(555) 555-5555", "(444) 444-4444"]
    }]
}

为了实现我在代码之后所做的功能。

public function actionSyncPhoneBook()
{
    $amResponse = $amResponseData = [];
    $snUserId   = $this->amData['user_id'];// Here i will get user_id
    if (!empty($this->omUser))
    {
        $userDetails                       = $this->amData['user_details'];//Here i will get array of user_details
        $amAppContacts =[];
        $amNonAppContacts = [];
        //I have loop here of user details to scan mobile number array
        foreach ($userDetails as $userKeys) 
        {
            //In case if mobile number array is not given  
            if(!empty($userKeys["mobile_number"]))
            {
                // Loop for scanning mobile numbers
                foreach ($userKeys["mobile_number"] as $key => $mobileNumber) 
                {
                    //regex for matching mobile number.
                    $phone = preg_replace('/[^a-zA-Z0-9+]/', '', $mobileNumber);
                    $oModelUser = Users::find()
                        ->where(['phone' => $phone])
                        ->one();
                        //Check if conttact is exists or not using exist method
                        $checkContactExists                        = Users::find()->where(['phone' => $phone])->exists();
                        if($checkContactExists                     == 1)
                        {
                            $oModelFriends                             = Friends::find()->where(["from_user_id"=>$snUserId,"to_user_id"=>$oModelUser->id])->asArray()->one();   
                            $amCheckPhoneNumber['user_id']             = !empty((string)$oModelUser->id) ? (string)$oModelUser->id  : '';
                            $amCheckPhoneNumber['first_name']          = !empty($oModelUser->first_name) ? $oModelUser->first_name  : '';
                            $amCheckPhoneNumber['last_name']           = !empty($oModelUser->last_name) ? $oModelUser->last_name  : '';
                            $amCheckPhoneNumber['email']               = !empty($oModelUser->email) ? $oModelUser->email  : '';
                            $amCheckPhoneNumber['phone']               = !empty($oModelUser->phone) ? $oModelUser->phone  : '';
                            $amCheckPhoneNumber['status']              = !empty((string)$oModelUser->status) ? (string)$oModelUser->status  : '';
                            $amCheckPhoneNumber['relationship_status'] = !empty($oModelUser->relationship_status) ? $oModelUser->relationship_status  : '';
                            $amCheckPhoneNumber['mobile_verified']     = !empty((string)$oModelUser->mobile_verified) ? (string)$oModelUser->mobile_verified  : '';
                            $amCheckPhoneNumber['email_verified']      = !empty($oModelUser->email_verified) ? $oModelUser->email_verified  : '';
                            $amCheckPhoneNumber['latitude']            = !empty($oModelUser->latitude) ? $oModelUser->latitude  : "0";
                            $amCheckPhoneNumber['longitude']           = !empty($oModelUser->longitude) ? $oModelUser->longitude  : "0";
                                if($oModelFriends["is_friend"] == 1)
                                {
                                        $amCheckPhoneNumber['is_friend']           = "friends";
                                }
                                    else
                                {
                                        $amCheckPhoneNumber['is_friend']           = "no_friends";
                                }

                            //User Image 
                            if(!empty($oModelUser->user_image))
                            {
                                if (filter_var($oModelUser->user_image, FILTER_VALIDATE_URL)) 
                                { 
                                    if(!empty($oModelUser->user_image))
                                    {
                                        $ssEventDetailsUserImage = $oModelUser->user_image;
                                    }
                                    else
                                    {
                                        $ssEventDetailsUserImage = Yii::$app->params['aws_cover_image_path'];
                                    }
                                }
                                else
                                {
                                    $ssEventDetailsUserImage =Yii::$app->params['aws_cover_image_path'];
                                }
                            }
                            else
                            {
                                $ssEventDetailsUserImage = Yii::$app->params['aws_cover_image_path'];
                            }   
                            //Cover  Image 
                            if(!empty($oModelUser->cover_image))
                            {
                                if (filter_var($oModelUser->cover_image, FILTER_VALIDATE_URL)) 
                                { 

                                    if(!empty($oModelUser->cover_image))
                                    {

                                        $ssCoverImage = $oModelUser->cover_image;
                                    }
                                    else
                                    {
                                        $ssCoverImage = Yii::$app->params['aws_cover_image_path'];
                                    }
                                }
                                else
                                {
                                    $ssCoverImage =Yii::$app->params['aws_cover_image_path'];
                                }
                            }
                            else
                            {
                                $ssCoverImage = Yii::$app->params['aws_cover_image_path'];
                            }                       
                            $amCheckPhoneNumber['cover_image']     = !empty($ssCoverImage) ? $ssCoverImage  : '';
                            $amCheckPhoneNumber['user_image']      = !empty($ssEventDetailsUserImage) ? $ssEventDetailsUserImage  : '';
                            $amCheckPhoneNumber['app_user'] = "1";
                            //Final Response of users those who are using application
                            $amAppContacts[]         = $amCheckPhoneNumber;
                    }
                    else
                    {
                          //Users who does not use application.
                          $otherUsers['user_id']             = !empty($userKeys['id']) ? $userKeys['id']  : '';
                          $otherUsers['first_name']          = !empty($userKeys['first_name']) ? $userKeys['first_name']  : '';
                          $otherUsers['last_name']           = !empty($userKeys['last_name']) ? $userKeys['last_name']  : '';
                          $otherUsers['email']               = !empty($userKeys['email']) ? $userKeys['email']  : '';
                          $otherUsers['phone']               = !empty($mobileNumber) ? $mobileNumber : '';
                          $otherUsers['status']              = !empty($userKeys['status']) ? $userKeys['status']  : '';
                          $otherUsers['relationship_status'] = !empty($userKeys['relationship_status']) ? $userKeys['relationship_status']  : '';
                          $otherUsers['user_image']          = Yii::$app->params['aws_cover_image_path'];
                          $otherUsers['cover_image']         = Yii::$app->params['aws_cover_image_path'];
                          $otherUsers['mobile_verified']     = !empty($userKeys['mobile_verified']) ? $userKeys['mobile_verified']  : '';
                          $otherUsers['email_verified']      = !empty($userKeys['email_verified']) ? $userKeys['email_verified']  : '';
                          $otherUsers['latitude']            = !empty($userKeys["latitude"]) ? $userKeys["latitude"]  : "0";
                          $otherUsers['longitude']           = !empty($userKeys["longitude"]) ? $userKeys["longitude"]  : "0";
                          $otherUsers['is_friend']           = "";
                          $otherUsers['app_user']     = "0";
                          //Final Response of users those who are not using  app.
                          $amNonAppContacts[] = $otherUsers;
                    }
                }
            }
        }
        //Merging the app users and non-app users together.
        $amResponseData = array_merge($amAppContacts,$amNonAppContacts);
        $amResponse     = Common:: successResponse("Sync your phonebook.",$amResponseData);                
        Common::encodeResponseJSON($amResponse);
    }
}

从上面的代码我正在检查数据库中是否存在移动号码,而不是推送到应用用户,并且其余部分将位于非应用用户中。

在我的数据库中,手机号码以+(countryCode)(mobileNumber)

的格式存储
  

示例 +918000584123

但如果我没有传递+91或任何国家代码,它也应该有用。

如果手机号码阵列就像

那么它应该有效
"mobile_number": ["(888) 888-888", "(777) 777-777"] 

或者它应该像

"mobile_number": ["+918000584123","+91 8000584123","8000584123", "(666) 666-6666", "(555) 555-5555", "(444) 444-4444"]

如何实现此功能?

任何帮助都将不胜感激。

提前致谢。

2 个答案:

答案 0 :(得分:1)

不需要像这样的正则表达式

$phone = preg_replace('/[^a-zA-Z0-9+]/', '', $mobileNumber);

相反,您可以使用

$phone = preg_replace('/[^0-9]/', '', $mobileNumber);

并在下面更改您的位置条件。你应该使用LIKE条件

$oModelUser = Users::find()->where("phone LIKE'%".$phone."'")->one();    

$checkContactExists = Users::find()->where("phone LIKE '%".$phone."'")->exists();

答案 1 :(得分:1)

您使用setup: function (editor) { ed.addButton('example', { title: 'example.desc', image: './/', text: 'Oferta', icon: true, onclick: function () { var h1 = editor.dom.create('h1'); h1.innerText = 'test'; var oferta = editor.dom.create('div' ,{'class': 'col-sm-3'}); oferta.appendChild(h1); var divs = editor.dom.select('div'); if(divs && divs.length > 0){ editor.dom.insertAfter(oferta,divs[divs.length-1]) }else{ tinyMCE.execCommand('mceInsertContent', false,oferta.outerHTML); } editor.selection.select(oferta); editor.selection.collapse(true); } }); }, 标记了它,所以我从这个角度回答。

各种电话号码应与单独的表一起,以及他们所属人员的ID。这样,任何人都可以有任意数量的数字。我建议1:很多,不是很多:很多,只是让偶尔的共享电话成为新表中的重复号码。

该表对数字有一个非唯一索引,因此在SQL中使用mysql找到具有给定数字(或一组数字)的人员是“微不足道的”。