2

I am using the following code to capitalize each word in a sentence, but I am unable to capitalize words with brackets attached.

PHP Code:

  <?php
     $str = "[this is the {command line (interface ";
     $output  = ucwords(strtolower($str));
     echo $output;

Output:

[this Is The {command Line (interface

But my expected output should be:

[This Is The {Command Line (Interface

How can I handle words with brackets? There may be multiple brackets.

For example:

[{this is the ({command line ({(interface

I want to find a general solution/function in PHP.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Raheel Aslam
  • 444
  • 1
  • 9
  • 28

2 Answers2

3
$output = ucwords($str, ' [{(');
echo $output;
// output ->
// [This Is The {Command Line (Interface

Update: general solution. Here a "bracket" - is any non-letter character. Any letter, following the "bracket" is converted to uppercase.

$string = "test is the {COMMAND line -STRET (interface 5more 9words #here";
$strlowercase = strtolower($string);

$result = preg_replace_callback('~(^|[^a-zA-Z])([a-z])~', function($matches)
{
    return $matches[1] . ucfirst($matches[2]);
}, $strlowercase);


var_dump($result);
// string(62) "Test Is The {Command Line -Stret (Interface 5More 9Words #Here"

Live demo

Joe Black
  • 867
  • 1
  • 9
  • 10
  • probably don't want the `strtolower`. What happens to `CLI` or `PHP`? – AbraCadaver Jun 05 '17 at 17:48
  • Other one I want to contain number with start word how to do it. example 3s [this is command line {interface in this case 's' should be capital. – Raheel Aslam Jun 06 '17 at 03:57
  • I have added $output = ucwords($str, ' [{(123456789'); but i have more brackets above 15.. Have proper solution that handle all brackets without define.because user can add any bracket that's not define in my code. – Raheel Aslam Jun 06 '17 at 04:04
  • @Joe Black But this "-STRET" letter is not converting into "-Stret" . Please see this. first i need all letter in lower case then first letter in uppercase. – Raheel Aslam Jun 06 '17 at 07:15
  • @RaheelAslam Added `strtolower()`. Check it, please – Joe Black Jun 06 '17 at 07:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145937/discussion-between-joe-black-and-raheel-aslam). – Joe Black Jun 06 '17 at 07:23
  • @Joe Black please check this example and verify not working after testing http://sandbox.onlinephpfunctions.com/code/0d28f30fe13ce69b0cc421aea1d4d6ad5fbe9938 – Raheel Aslam Jun 08 '17 at 23:04
1

This is another solution, you can add more delimiters in the for-each loop array if you want to handle more characters.

function ucname($string) {
    $string =ucwords(strtolower($string));

    foreach (array('-', '\'') as $delimiter) {
      if (strpos($string, $delimiter)!==false) {
        $string =implode($delimiter, array_map('ucfirst', explode($delimiter, $string)));
      }
    }
    return $string;
}
?>
<?php
//TEST

$names =array(
  'JEAN-LUC PICARD',
  'MILES O\'BRIEN',
  'WILLIAM RIKER',
  'geordi la forge',
  'bEvErly CRuSHeR'
);
foreach ($names as $name) { print ucname("{$name}\n<br />"); }

//PRINTS:
/*
Jean-Luc Picard
Miles O'Brien
William Riker
Geordi La Forge
Beverly Crusher
*/
Yulio Aleman Jimenez
  • 1,642
  • 3
  • 17
  • 33