?
Solved

I need to add a ‘s if $variable does no end with a digit.

Posted on 2005-02-28
4
Medium Priority
?
231 Views
Last Modified: 2010-04-17
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";
0
Comment
Question by:blnukem
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13419700
<?
    $variable = "Hot Dog";
    $lastDigit = substr( $variable , strlen( $variable ) - 1 );
    if( ! is_numeric( $lastDigit ) )
    {
        $variable .= "’s";
    }
    echo $variable;
?>
0
 

Author Comment

by:blnukem
ID: 13419722
In Perl
0
 
LVL 32

Accepted Solution

by:
ldbkutty earned 2000 total points
ID: 13419863
I dont perl, but the same logic should be applied for ALL languages.

Try this for Perl :

    $variable = "Hot Dog";
    $lastDigit = substr( $variable , length( $variable ) - 1 );
    if( $lastDigit !~ /0123456789/ )
    {
        $variable = $variable . "’s";
    }
    print $variable;

// there might be any syntax errors, but the logic should work.
0
 

Author Comment

by:blnukem
ID: 13419970

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;
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Simple Linear Regression
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question