2013-08-30 195 views
-4
if($title=="Random 1.5"){ //value1 
    $ytitle = "Custom 1.5"; 
    } 
else if($title=="Another 1.6"){ //value2 
    $ytitle = "Custom 1.6"; 
    } 
else if($title=="Bold Random 1.5"){ //value3 
    $ytitle = "Custom 1.7"; 
    } 

值1和值3的檢索真實的,因爲(隨機1.5)在字符串。如何解決這個問題?我只想發帖加粗隨機1.5值。謝謝你的幫助。多串漏洞,如果在else語句

+0

這是什麼語言? 'php'? – Rotem

+0

是語言是php – user2733659

回答

2

你正在做精確的字符串匹配,而不是子字符串匹配,所以除非你的$title的值與if()語句中的字符串完全相同,否則你的「隨機1.5」和「粗體隨機1.5 「會匹配相同的。

例如

$teststring = 'Random 1.5'; 

($teststring == 'Random 1.5') // evaluates to TRUE 
($teststring == 'Bold Random 1.5') // evaluates to FALSE 

,但如果你有

strpos('Random 1.5', $teststring) // integer 0 result, not boolean false 
strpos('Bold Random 1.5', $teststring) // integer 4 result, not boolean false 

兩個都會成功,因爲 '隨機1.5' 在被搜索兩個字符串顯示出來。

而且,因爲你反覆對多個值測試一個變量,可以考慮使用一個開關()代替:

switch($title) { 
    case 'Random 1.5':  $ytitle = 'Custom 1.5'; break; 
    case 'Another 1.6':  $ytitle = 'Custom 1.6'; break; 
    case 'Bold Random 1.5': $ytitle = 'Custom 1.7'; break; 
} 
+0

感謝您的幫助。工作正常。 – user2733659