简体   繁体   English

PHP字符串与str_replace的比较不起作用

[英]PHP String Comparison With str_replace not working

I'm trying to compare a few strings to execute an if statement. 我正在尝试比较一些字符串以执行if语句。 I've tried a few ways around using str_replace and trim to remove all white spaces but it does not work when there are several words. 我已经尝试过使用str_replacetrim删除所有空白的几种方法,但是当有多个单词时它不起作用。 Any suggestion? 有什么建议吗? Thanks. 谢谢。

string

//check company industry
$checkcompanyq = "SELECT type,id,employee_id FROM company";
$checkcompanyresult = mysqli_query($db, $checkcompanyq);
$companyassoc = mysqli_fetch_assoc($checkcompanyresult);
$companyindustry = $companyassoc['type'];
$comindustrynospace  = str_replace(' ','',$companyindustry);

It works when it's a single word. 一个单词就可以使用。 This string will work and if statement execute 该字符串将起作用,并且如果执行语句

if($comindustrynospace=="Insurance"){
//do something
}

It does not work when several words. 几个单词时不起作用。 This string wont work and if statement wont execute 此字符串将不起作用,并且if语句将不会执行

 if($comindustrynospace=="RealEstate&Rental"){
    //do something
    }

I tried with trim but does not execute either 我试过修剪但不执行

 if(trim($comindustrynospace)==trim("RealEstate&Rental")){
        //do something
        }

This is what $companyindustry looks like before str_replace 这就是$ companyindustry在str_replace之前的样子

$companyindustry = 'Real Estate & Rental';

The following code works. 以下代码有效。 You should check $comindustrynospace and the value you want to compare with. 您应该检查$comindustrynospace及其要比较的值。

$companyindustry=" My Insurance ";
$comindustrynospace = str_replace(' ','',$companyindustry);

echo $comindustrynospace;

if($comindustrynospace=="MyInsurance"){
//do something
  echo " <br>Insurance work";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM