为了账号安全,请及时绑定邮箱和手机立即绑定

如何在php中过滤字符串

如何在php中过滤字符串

PHP
潇湘沐 2022-08-19 10:38:05
我想制作Web应用程序:我发送 address.in 后端,我们最删除标题和不必要的详细信息,以exp其我的地址Home: Tehran،enqelab str،khark str(Call me)之后发送到我的api并验证地址是真是假它是我的第一个代码,但它发送到许多请求apipublic function check($id)    {        $address = Address::find($id);        $addres = $address->Address;       $array = $this->ValidateArray($address->Address);        $FinalAddress = implode(" ", $array);        echo $FinalAddress;        echo "<br/><br/><br/><br/><br/>";        $response = $this->CallApi($FinalAddress);        while ($response == null) {            array_splice($array, -1);            $FinalAddress = implode(" ", $array);            $response = $this->CallApi($FinalAddress);        }        while ($response->num < 1) {            array_splice($array, -1);            $FinalAddress = implode(" ", $array);            $response = $this->CallApi($FinalAddress);        }        $address->FoundedAddress = $response->result[0]->title;        $address->Status = 2;        $address->save();        echo  $response->result[0]->title;        echo "<br/><br/><br/><br/>";        echo $FinalAddress;        echo "<br/><br/><br/><br/>";        var_dump($response);    } public function ValidateArray($address)    {        $first = str_replace("،", " ", $address);        $second = str_replace("-", " ", $first);        $third = str_replace("(", " ", $second);        $Fourth = str_replace(":", " ", $third);        $Final = str_replace(")", " ", $Fourth);        $array = explode(" ", $Final);        return $array;    }
查看完整描述

2 回答

?
蓝山帝景

TA贡献1843条经验 获得超7个赞

检查您下面的代码,该代码正在发送多次请求。


while ($response == null) {

    array_splice($array, -1);

    $FinalAddress = implode(" ", $array);

    $response = $this->CallApi($FinalAddress);

}

while ($response->num < 1) {

    array_splice($array, -1);

    $FinalAddress = implode(" ", $array);

    $response = $this->CallApi($FinalAddress);

}


查看完整回答
反对 回复 2022-08-19
?
泛舟湖上清波郎朗

TA贡献1818条经验 获得超3个赞

我找到了我的遮阳篷


 public function check($id)

    {

//find addres from dataabase

        $address = Address::find($id);

        $Address = $address->Address;

//check for its address or no

        if (strpos($Address, 'STR') || strpos($Address, 'S ') || strpos($Address, "Alley") || strpos($Address, "A ") || strpos($Address, 'number ') !== false) {

//validate address and remove unnecessary details 

            $FinalAddress = $this->ValidateArray($Address);

//call api

            $response = $this->CallApi($FinalAddress);

//update database

            $address->FoundedAddress = $response->result[0]->title;

            $address->Status = 2;

            $address->save();

            echo "Searched Address :‌ ". $FinalAddress;

            echo "<br/><br/><br/><br/><br/>";

            echo "Founded Address :‌ " . $response->result[0]->title;

        }else{

            echo "Address Not Valid";

        }

    }

和验证函数


//remove ، from address

if (strpos($address, "،")) {

            $address = explode("،", $address);

            $address = implode(" ", $address);

        }

//remove - from address

        elseif (strpos($address, "-")) {

            $address = explode("-", $address);

            $address = implode(" ", $address);

        }

remove title and : from address

        if (strpos($address, ":")) {

            $address = strstr($address, ':');

            $address = str_replace(":", "", $address);

        }

//we found the (anyting) and remove that

        if (strpos($address , ")") ){

            $start = "(";

            $end = ")";

            $replace = " ";

            $pos1 = strpos($address , $start);

            $pos2 = strpos($address , $end , $pos1);

            $lenght = $pos2 + strlen($pos1) - $pos1;

            $address = substr_replace($address , $replace , $pos1 , $lenght);

        }

//remove number from address

        if (strpos($address, "number")) {

            $address = substr($address, 0, strpos($address, "nmber"));

        }

//remove floor from address

        if (strpos($address, "floor")) {

            $address = substr($address, 0, strpos($address, "floor"));

        }


查看完整回答
反对 回复 2022-08-19
  • 2 回答
  • 0 关注
  • 112 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信