6

How can I capitalize only the last letter of a string.

For example:

hello

becomes:

hellO
Brad
  • 159,648
  • 54
  • 349
  • 530
Kathy
  • 61
  • 1
  • 2

6 Answers6

14

Convoluted but fun:

echo strrev(ucfirst(strrev("hello")));

Demo: http://ideone.com/7QK5B

as a function:

function uclast($str) {
    return strrev(ucfirst(strrev($str)));
}
karim79
  • 339,989
  • 67
  • 413
  • 406
  • That works great! when I enter it just like that, but for what I'm trying to do is in wordpress change the page titles to all lowercase and the last letter uppercase. for example: right now the title shows up as Sample Page. I want it to show up as sample pagE . heres what I tried, but its not working `code` – Kathy Jul 26 '11 at 00:51
  • 1
    @Kathy: `$str = strtolower(the_title());` first then. If it doesn't work, there would likely be an error in your `the_title()` function. – bob-the-destroyer Jul 26 '11 at 01:00
  • ya you would think it would work. I'm not sure why its not. I would change all the page titles so they are entered with the cap at the end but i dont want it to show up that way in the navigation. I'm not sure how to do a cache break?? That link takes me to something about adsense. Thanks so much for your help! – Kathy Jul 26 '11 at 01:00
  • @Kathy - what Bob said. Sorry, I didn't get your comment. Just use `strtolower` first. Thanks @bob-the-destroyer. – karim79 Jul 26 '11 at 01:02
  • 2
    better use `get_the_title()` to have a return value. `the_title()` will echo. Or do `the_title('','', false);` for the return value. – hakre Jul 26 '11 at 01:05
  • @bob-the-destroyer It's wordpress template tags. http://codex.wordpress.org/Function_Reference/get_the_title. edit - removed lecturing – calumbrodie Jul 26 '11 at 01:13
  • @bob-the-destroyer - I think that's wordpress-speak. – karim79 Jul 26 '11 at 01:13
  • Thanks guys, I think you're right. I think its the the_title() function. I think its already capitalizing the first letter, so I'll see if i can find where it is and change it from there:) I'll let you know! Thanks again:) – Kathy Jul 26 '11 at 01:14
  • @hakre thank you, using get_the_title() actually worked! so i didnt have to go look for the title function !! thanks again you guys..what an awesome forum. I will be back i'm sure! – Kathy Jul 26 '11 at 01:19
3

When $s is your string (Demo):

$s[-1] = strtoupper($s[-1]);

Or in form of a function:

function uclast(string $s): string
{
  $s[-1] = strtoupper($s[-1]);
  return $s;
}

And for your extended needs to have everything lower-case except the last character explicitly upper-case:

function uclast(string $s): string
{
  if ("" === $s) {
    return $s;
  }

  $s = strtolower($s);
  $s[-1] = strtoupper($s[-1]);

  return $s;
}
hakre
  • 193,403
  • 52
  • 435
  • 836
  • @Martijn: https://3v4l.org/15aT3 looks fine 8.0.0 - 8.0.9. and since 7.1.0 there is the -1 optimization, I should update the answer: https://3v4l.org/9Lj4c – hakre Aug 10 '21 at 22:53
1

There are two parts to this. First, you need to know how to get parts of strings. For that, you need the substr() function.

Next, there is a function for capitalizing a string called strtotupper().

$thestring="Testing testing 3 2 1. aaaa";
echo substr($thestring, 0, strlen($thestring)-2) . strtoupper(substr($thestring, -1));
Brad
  • 159,648
  • 54
  • 349
  • 530
0

Lowercase / uppercase / mixed character case for everything following can be used

<?php
    $word = "HELLO";

    //or

    $word = "hello";

    //or

    $word = "HeLLo";

    $word = strrev(ucfirst(strrev(strtolower($word))));

    echo $word;
?>

Output for all words

hellO
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89
0
$string = 'ana-nd';

echo str_replace(substr($string, -3), strtoupper('_'.substr($string, -2)), $string);

// Output: ana_ND


$string = 'anand';

echo str_replace(substr($string, -2), strtoupper(substr($string, -2)), $string);

// Output: anaND
halfelf
  • 9,737
  • 13
  • 54
  • 63
0

Here's an algorithm:

  1. Split the string s = xyz where x is the part of
     the string before the last letter, y is the last
     letter, and z is the part of the string that comes
     after the last letter.
  2. Compute y = Y, where Y is the upper-case equivalent
     of y.
  3. Emit S = xYz
Patrick87
  • 27,682
  • 3
  • 38
  • 73