blnukem
asked on
I need to add a ‘s if $variable does no end with a digit.
I need to add a ‘s if $variable does no end with a digit.
GOOD EXAMPLE:
$variable = "Hot Dog";
GOOD RESULT:
$variable = "Hot Dog’s";
BAD EXAMPLE:
$variable = "Call me at 123456";
BAD RESULT:
$variable = "Call me at 123456";
GOOD EXAMPLE:
$variable = "Hot Dog";
GOOD RESULT:
$variable = "Hot Dog’s";
BAD EXAMPLE:
$variable = "Call me at 123456";
BAD RESULT:
$variable = "Call me at 123456";
ASKER
In Perl
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Got it with this thanks!
$variable = "Hot Dog";
$lastDigit = substr( $variable , length( $variable ) - 1 );
if( $lastDigit =~ m/[a-zA-Z]/ ){
$variable = $variable . "’s";
}
print $variable;
$variable = "Hot Dog";
$lastDigit = substr( $variable , strlen( $variable ) - 1 );
if( ! is_numeric( $lastDigit ) )
{
$variable .= "’s";
}
echo $variable;
?>