2015-06-03 66 views
-2

1.我有兩個字符串1.abc和2.abc_1,我比較這兩個字符串第二個字符串有兩個額外字符,比較後如何返回這些字符在PHP中。 2.如果字符串不以「_1」結尾,如果字符串以「_1或(任何數字值)」結尾,我想添加「_1」,我想爲該值添加+1,如何在php中執行此操作。如何比較兩個字符串如果最後兩個字符不匹配則返回那些字符

$jobref='abc'; 
$newjobref='abc_1'; 
if(strcasecmp($jobref,$newjobref)==0) 
{ 
    //here i want to add _1 to that string 
} 
else 
{ 
    //return last two character 
    if(last character == (any numeric value)) 
    { 
    //add plus 1 to that value 
    } 
} 

回答

3

你要做的就是用explode返回字符串的終值,那麼你就可以在if/else循環編輯值,使finalblow最終結果。

這花了大約6分鐘的時間來閱讀,研究和完成,它沒有經過測試,但我認爲你需要做的是將你的問題分解成部分,然後研究個別部分以解決問題。

任何問題,讓我知道:)

$blowref = "abc"; 
$newblowref = "abc_1"; 

if(strcasecmp($blowref,$newblowref)==0) 
{ 
    $finalblow = $blowref."_1"; 
} 
else 
{ 
    //return last two character 
    $bog = explode("_",$blowref); 
    $bog = end($bog); //the final part of the string after the `_` character 
    if(is_numeric($bog) || $bog === 0) 
    { 
    $finalblow = str_replace($bog,($bog+1),$blowref); 
    } 
} 

與感謝/點頭聖公會的爆炸概念。

+0

感謝您的代碼讓我試試這一個 –

+0

是否奏效,@ M.Athishkrishna? – Martin

+0

nope根據我的招聘人數進行一些更正 –

2

如果字符串不包含下劃線加上_1,如果它包含它,explode就可以了,增量和implode?爆炸將保證完整的數字。

+1

這是一個好的觀點。 – Martin

相關問題