dongquanlin1885 2015-06-03 12:40
浏览 65

如果最后两个字符不匹配,如何比较两个字符串然后返回那些字符

1.I have two string 1.abc and 2.abc_1 and i am comparing these two string 2nd string has two extra character, after comparing how to return these character in php. 2.If the string not ends with "_1" i want to add "_1" if the string ends with "_1 or (any numeric value)" i want to add +1 to that value,how to do this in 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
  }
}
  • 写回答

2条回答 默认 最新

  • douyun1950 2015-06-03 12:50
    关注

    What you do is use explode to return the final value of the string, then you can edit that value in the if/else loop and make finalblow the final outcome.

    This took about 6 minutes of time to read, research and complete, it's untested but I think what you need to do is break your question into parts and then research individual parts down to solve the question.

    Any issues let me know :)

    $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);
      }
    }
    

    With thanks/nod to skh for the explode concept.

    评论

报告相同问题?