简体   繁体   中英

PHP String Comparison With str_replace not working

I'm trying to compare a few strings to execute an if statement. 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. 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($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 = 'Real Estate & Rental';

The following code works. You should check $comindustrynospace and the value you want to compare with.

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

echo $comindustrynospace;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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