Link to home
Start Free TrialLog in
Avatar of blnukem
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";
Avatar of ldbkutty
ldbkutty
Flag of India image

<?
    $variable = "Hot Dog";
    $lastDigit = substr( $variable , strlen( $variable ) - 1 );
    if( ! is_numeric( $lastDigit ) )
    {
        $variable .= "’s";
    }
    echo $variable;
?>
Avatar of blnukem
blnukem

ASKER

In Perl
ASKER CERTIFIED SOLUTION
Avatar of ldbkutty
ldbkutty
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of blnukem

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;