Jeremy
Jeremy

Reputation: 2536

PHP returning a string with the first char of every words in lowercase

There is already a function in PHP called ucwords which does just the opposite of what i need.

Is there such php library called lcwords? where instead of capitalizing the first of every words it converts them to lower case.

Thanks.

Upvotes: 2

Views: 2007

Answers (8)

Devaldo
Devaldo

Reputation: 111

I know the topic is ancient, but the problem still persist which is a shame (no lcwords() even now).

Here is lcwords() to lowercase each first letter of each word.

Simply put: this solution is universal and any punctuation should not be a problem for it. Of course, you pay you price for it :)


    /**
     * Lowercase the first character of each word in a string
     *
     * @param  string $string     The input string.
     * @param string $delimiters The optional delimiters contains the word separator characters (regular expression)
     *
     * @return string             Returns the modified string.
     */
    function lcwords($string, $delimiters = "\W_\t\r\n\f\v") {
        $string = preg_replace_callback("/([$delimiters])(\w)/", function($match) {
            return $match[1].lcfirst($match[2]);
        }, $string);

        return lcfirst($string); // Uppercase first char if it's the beginning of the line
    }

    // Here is a couple of result examples:

    echo lcwords("/SuperModule/ActionStyle/Controller.php").PHP_EOL;
    // result:    /superModule/actionStyle/controller.php

    echo lcwords("SEPARATED\tBY TABS\nAND\rSPACES").PHP_EOL;
    // result:    sEPARATED  bY tABS  aND  sPACES

    echo lcwords("HELLO").PHP_EOL;
    // result:    hELLO

    echo lcwords("HEELO HOW-ARE_YOU").PHP_EOL;
    // result:    hEELO hOW-aRE_yOU

    echo lcwords("SEPARATED\tBY TABS\nAND\rSPACES").PHP_EOL;
    // result:    sEPARATED  bY tABS  aND  sPACES

/([$delimiters])(\w)/ - Pattern have two groups: search for 1 non word character followed by 1 word character. Then word character will be uppercased in a callback. So only selected set of characters are getting updated - minimum changes of the content.

Upvotes: 0

ankr
ankr

Reputation: 667

function lcwords(string $str) :string
{
    return preg_replace_callback('/(?<=^|\s)\w/', function (array $match) :string {
        return strtolower($match[0]);
    }, $str);
}

Upvotes: 0

Cl&#225;udio Silva
Cl&#225;udio Silva

Reputation: 915

An even shorter one-liner:

implode(' ',array_map('lcfirst',explode(' ',$text)))

Upvotes: 0

Kevin Johnson
Kevin Johnson

Reputation: 1970

Here is a one liner:

implode(' ', array_map(function($e) { return lcfirst($e); }, explode(' ', $words)))

Example:

function lcwords($words) {
  return implode(' ', array_map(function($e) { return lcfirst($e); }, explode(' ', $words)));
}

$words = "First Second Third";
$lowercased_words = lcwords($words);
echo($lowercased_words);

Upvotes: 2

Oskar
Oskar

Reputation: 139

I had to give it a shot with regex:

<?php
$pattern = "/(\b[a-zA-Z])(\w)/";
$string = "ThIs is A StriNG x y z!";

// This seems to work 
$result = preg_replace_callback($pattern, function($matches) {
    return (strtolower($matches[1]).$matches[2]);
}, $string);

echo $result;
echo "\r\n";

//This also seems to do the trick. Note that mb_ doesn't use / 
echo mb_ereg_replace('(\b[a-zA-Z])(\w{0,})', "strtolower('\\1') . '\\2'", $string, 'e');

// I wanted this to work but it didn't produce the expected result:
echo preg_replace($pattern, strtolower("\$1") . "\$2", $string);
echo "\r\n";

Upvotes: 0

randomizer
randomizer

Reputation: 1649

$string = "THIS IS SOME TEXT";
$string=explode(" ",$string);
$i=0;
while($i<count($string)){
    $string[$i] = lcfirst($string[$i]);
    $i++;
}
echo implode(" ",$string);

Found another function at this link.

Upvotes: 1

coder
coder

Reputation: 701

This may be helpful to you

$str="hello";
$test=substr($str, 0,1);
$test2=substr($str, 1,strlen($str));
echo $test.strtoupper($test2);

Upvotes: 1

calexandru
calexandru

Reputation: 307

"Lowercase the first character of each word in a string php" in google and this is the first response that you get: http://php.net/manual/en/function.lcfirst.php

Upvotes: -2

Related Questions