1

I am using ucword function to uppar case only first word as this ucwords(strtolower($var)) but sometimes I want the word to be uppar case. Please see the below example to understand clearly.

$var = "class iii";
echo ucwords(strtolower($var));
gives as **Class Iii or Class Ii or Class Iv**

What I want it should display as Class III or Class II or Class IV to look beautiful

majidarif
  • 18,694
  • 16
  • 88
  • 133

6 Answers6

4

You can achieve this using preg_replace_callback() with a regex that uses positive lookahead:

/\b(?=[LXIVCDM]+\b)([a-z]+)\b/i

Explanation:

  • \b - assert position at a word boundary
  • (?= - positive lookahead
    • [LXIVCDM]+ - match any character from the list one or more times
    • \b - assert position at a word boundary
  • ) - end of positive lookahead
  • [a-z] - any alphabet
  • \b - assert position at a word boundary
  • i - pattern modifier that makes the matching case-insensitive

Code:

$str = "class iii";

$string = preg_replace_callback('/\b(?=[LXIVCDM]+\b)([a-z]+)\b/i', 
function($matches) {
    return strtoupper($matches[0]);
}, ucwords(strtolower($str)));

echo $string;

Output:

Class III

Demo

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
1

Try using this instead:

<?php

    function xucwords($string)
    {
        $words = split(" ", $string);
        $newString = array();

        foreach ($words as $word)
        {
            if(!preg_match("/^m{0,4}(cm|cd|d?c{0,3})(xc|xl|l?x{0,3})(ix|iv|v?i{0,3})$/", $word)) {
                $word = ucfirst($word);
            } else {
                $word = strtoupper($word);
            }

            array_push($newString, $word);
        }

        return join(" ", $newString);  
    }

    echo xucwords('class iii');

?>

based on the function found here.

majidarif
  • 18,694
  • 16
  • 88
  • 133
  • 3
    If you are copying from the source http://titanic.fauser.edu/php/function.strtolower.php.htm , you need to specify the link ! – Shankar Narayana Damodaran Apr 16 '14 at 05:58
  • @ShankarDamodaran I did. – majidarif Apr 16 '14 at 05:59
  • could you please tell me what is in $lower_exceptions array ?? – user3477550 Apr 16 '14 at 05:59
  • @user3477550 It is just to keep those list of words lower case. Like `a` or `the` will always be lower case. – majidarif Apr 16 '14 at 06:00
  • @user3477550 I modified the code and removed the lower_exceptions. – majidarif Apr 16 '14 at 06:09
  • Hi Majimboo, I tried this but the resule is same as ucword. `echo xucwords("class iii"); echo xucwords("class iv"); echo xucwords("class bla bla iii"); echo xucwords("final exam iii");` gives me `Class IiiClass IvClass Bla Bla IiiFinal Exam Iii ` – user3477550 Apr 16 '14 at 06:13
  • Thank you very much dear, Its working perfect... I appreciate your hard work. – user3477550 Apr 16 '14 at 06:30
  • @majimboo: I noticed that you removed the credits a few minutes ago. Always give proper credit to the author and site where you found the text/code, including a direct link to it. Plagiarism isn't welcome on Stack Overflow (or on any SE site for that matter). – Amal Murali Apr 16 '14 at 06:41
  • @AmalMurali sorry about that. I was just editing my answer. Also there is now a difference between the code on the site and the code i posted. But anyways, noted. – majidarif Apr 16 '14 at 06:43
  • Change the `$words = split(" ", $string);` to `$words = split(" ", strtolower($string));`. Because if string is class Iii or class III will not work. – Inkeliz Jan 30 '16 at 15:25
0

You the below code and see my inline comments

$var = "class iii";

echo upperCaseCustomFunction($var);

upperCaseCustomFunction($string) {

    $arr = explode(" ",$string);

    $stringPartOne = ucwords(strtolower($arr[0])); // will return Class 

    $stringPartTwo = strtoupper(strtolower($arr[1]));   // Will return III 

    return $tringPartOne." ".$stringPartTwo; // will return Class III

}
opensource-developer
  • 2,826
  • 4
  • 38
  • 88
  • Dear, its just a example I have given, I, II, III its not always come on the $arr[1] position. Like Mid Exam III or Mid Exam IV – user3477550 Apr 16 '14 at 06:03
0

ucwords will convert only first character of string in uppercase.

You have to find for all iii or iv first convert it to uppercase and then apply ucowrds to whole string.

slavoo
  • 5,798
  • 64
  • 37
  • 39
sshet
  • 1,152
  • 1
  • 6
  • 15
-1

Simple use like below :

$var = strtolower("class ") . strtoupper("iii");
echo ucwords($var);

//Gives Class III

Pankaj Garg
  • 1,272
  • 15
  • 21
-1

Try this

<?php
$var = "class iii";
$var =  ucfirst(strtolower($var));
echo str_replace(substr($var, 6), strtoupper(substr($var, 6)), $var);
?>

Output:

Class III
Deepu Sasidharan
  • 5,193
  • 10
  • 40
  • 97