PHP/REGEX: Remove last letter from string if it is alone

Using PHP and regular expressions, how can I remove an orphan letter from the end of a string?
This is a test
should remain unchanged but
this is a test x
should have the x removed from the end.
LVL 10
skijAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
I would not use a regular expression for this.  Just convert the string to an array and unset the end of the array with array_pop().  It can be packaged as a function.
<?php // demo/temp_skij.php

/**
 * http://www.experts-exchange.com/questions/28732903/PHP-REGEX-Remove-last-letter-from-string-if-it-is-alone.html
 *
 */
error_reporting(E_ALL);
echo '<pre>';


// A FUNCTION TO TRUNCATE STRINGS THAT END WITH A SINGLE NON-BLANK CHARACTER, DELIMITED BY A SINGLE BLANK CHARACTER
function declop($str)
{
    $arr = explode(' ', trim($str));
    $end = end($arr);
    if (strlen($end) > 1) return $str;
    $end = array_pop($arr);
    return implode(' ', $arr);
}


// TEST THE FUNCTION
$tests =
[ 'This is a test x'
, 'This is a test'
, 'This is a test xx'
, 'This is a test x                 '
]
;
foreach($tests as $test)
{
    echo PHP_EOL . declop($test);
}

Open in new window

Ray PaseurCommented:
This seems to work, too.
<?php // demo/temp_skij.php

/**
 * http://www.experts-exchange.com/questions/28732903/PHP-REGEX-Remove-last-letter-from-string-if-it-is-alone.html
 *
 */
error_reporting(E_ALL);
echo '<pre>';


// A FUNCTION TO TRUNCATE STRINGS THAT END WITH A SINGLE NON-BLANK CHARACTER, DELIMITED BY A SINGLE BLANK CHARACTER
function declop($str)
{
    $rgx
    = '#'          // REGEX DELIMITER
    . '^'          // AT THE TOP OF THE STRING
    . '([^ ])'     // CAPTURE GROUP NON-BLANK
    . '{1}'        // EXACTLY ONE
    . ' '          // TRAILING BLANK
    . '{1}'        // EXACTLY ONE
    . '#'
    ;
    $rev = strrev(trim($str));
    $rev = preg_replace($rgx, NULL, $rev);
    return strrev($rev);
}


// TEST THE FUNCTION
$tests =
[ 'This is a test x'
, 'This is a test'
, 'This is a test xx'
, 'This is a test x                 '
]
;
foreach($tests as $test)
{
    echo PHP_EOL . declop($test);
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.