2

I have a function that is suppose to make the first character of all sentences uppercase, but for some reason, it's not doing it to the first character of the first sentence. Why is that happening, and how do I fix it?

<?php

function ucAll($str) {

$str = preg_replace_callback('/([.!?])\s*(\w)/', 
create_function('$matches', 'return strtoupper($matches[0]);'), $str);
return $str;

} //end of function ucAll($str)

$str = ucAll("first.second.third");
echo $str;

?>

Result:

first.Second.Third

Expected Result:

First.Second.Third
jessica
  • 1,667
  • 1
  • 17
  • 35

5 Answers5

1

It does not uppercase the first word because the regular expression requires there to be one of ., ! or ? in-front of it. The first word does not have one of those characters in-front of it.

This would do it:

function ucAll($str) {
    return preg_replace_callback('/(?<=^|[\.\?!])[^\.]/', function ($match) {
        return strtoupper($match[0]);
    }, $str);
}

It uses a positive look-behind to make sure one of ., !, ?, or the start of the line, are in-front of the matched string.

Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
0

Try this

function ucAll($str) {

$str = preg_replace_callback('/([.!?])\s*(\w)|^(\w)/', 
create_function('$matches', 'return strtoupper($matches[0]);'), $str);
return $str;

} //end of function ucAll($str)

$str = ucAll("first.second.third");
echo $str;

|^(\w) is "or get first character"

Tim007
  • 2,557
  • 1
  • 11
  • 20
0

Something like this :

    function ucAll($str) {
            $result = preg_replace_callback('/([.!?])\s*(\w)/',function($matches) {
            return strtoupper($matches[1] . ' ' . $matches[2]);
            }, ucfirst(strtolower($str)));
             return $result;

            } //end of function ucAll($str)
$str = ucAll("first.second.third");
echo $str;

Output :

First. Second. Third

Drudge Rajen
  • 7,584
  • 4
  • 23
  • 43
0

It is happening because your regular expression only matches characters after your defined set of punctuation marks, and the first word does not follow one of those. I would suggest making these changes:

First, this group ([?!.]|^) matches the beginning of the string (^) as well as your list of punctuation marks before the (optional) spaces and word character you are trying to replace. Setting it up this way means it should still work if there is any space at the beginning of the string.

Second, using an anonymous function instead of create_function is recommended if you are using PHP >= 5.3, which you hopefully are at this point (if you aren't, just changing the regex in your function should still work.)

function ucAll($str) {
    return preg_replace_callback('/([?!.]|^)\s*\w/', function($x) {
        return strtoupper($x[0]);
    }, $str);
}
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
0

I've updated your regex and used ucwords instead of strtoupper like as

function ucAll($str) {
    return preg_replace_callback('/(\w+)(?!=[.?!])/', function($m){
        return ucwords($m[0]);
    }, $str);
}
$str = ucAll("first.second.third");
echo $str;
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54