Skip to content

Instantly share code, notes, and snippets.

@Cvetomird91
Created June 18, 2017 07:57
Show Gist options
  • Save Cvetomird91/1ff9e34ba10736027cc62cc7723364ed to your computer and use it in GitHub Desktop.
Save Cvetomird91/1ff9e34ba10736027cc62cc7723364ed to your computer and use it in GitHub Desktop.
<?php
function lcwords($str) {
$temp = '';
$words = explode(' ', $str);
foreach ($words as $word) {
$word = ' ' . lcfirst($word);
$temp .= $word;
}
return ltrim($temp);
}
$str = "Hello World Asd";
$str = lcwords($str);
print_r($str);
@jojo1981
Copy link

jojo1981 commented Oct 2, 2021

Implementation can be must cleaner and written as a one liner:

    function lcwords(string $str): string {
        return \implode(' ', \array_map('lcfirst', \explode(' ', $str)));
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment