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
}
}
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.
If the string doesnt contain an underscore add the _1, if it contains it, explode
on it, increment and implode
? Exploding will guarantee a full number.