2015-06-03 70 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

相关问题