2017-12-12 41 views
0

if文を実行するためにいくつかの文字列を比較しようとしています。私はstr_replacetrimを使用してすべての空白を削除する方法をいくつか試しましたが、複数の単語がある場合は機能しません。なにか提案を?ありがとう。PHPの文字列比較str_replaceが機能していない

文字列

//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); 

それは一つの単語だとき、それは動作します。この文字列は動作し、if文が実行されます

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

複数の単語があると動作しません。この文字列は文句を言わない仕事とif文文句を言わない、私はトリムと試みたが、実行しません

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

実行のいずれか

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

これは$ companyindustryがstr_replace

$companyindustry = 'Real Estate & Rental'; 
+0

てみ[この](http://php.net/manual/en/function.stristr.php) – hungrykoala

+0

あなたの全体 '$のcompanyindustry'列過去できますか? –

+0

str_replaceが正常に動作しています** $ comindustrynospace ** var –

答えて

0

前に、次のように見えるものですコードが動作します。比較する値は$comindustrynospaceである必要があります。

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

echo $comindustrynospace; 

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