3

I would like to capitalize the first letter after a dot or after a dot and a space.

$string="I am a string with several periods.period #1. period #2.";

This should be the final string:

I am a string with several periods.Period #1. Period #2.

I have already searched for a solution on stackoverflow but the solution that i found was only for capitalize the initial letter after just a dot and not for a dot and a space.

Daniele
  • 129
  • 12
  • 1
    I find the duplicate incorrect. There is nothing in that answer that even mentions the problem OP explains, the optional number of spaces. – Andreas Nov 10 '18 at 20:48
  • @Andreas, you are right! I have also consulted the link of Wiktor before asking my question. – Daniele Nov 10 '18 at 20:56

4 Answers4

3

Preg_replace_callback is your friend:

$string="I am a string with several periods.period #1. period #2.";
$string = preg_replace_callback('/\.\s*\K\w/', 
    function($m) {
        return strtoupper($m[0]);
    },
    $string);
echo $string;    

Output:

I am a string with several periods.Period #1. Period #2.
Toto
  • 89,455
  • 62
  • 89
  • 125
1

Use regex to match the dot \., optional space \s* and a letter \w.
Then loop the matches array and do a str_replace.

$str="I am a string with several periods.period #1. period #2.";
preg_match_all("/\.\s*\w/", $str, $matches);

foreach($matches[0] as $match){
    $str = str_replace($match, strtoupper($match), $str);
}
echo $str;
//I am a string with several periods.Period #1. Period #2.

https://3v4l.org/LevU5

To make it slightly more optimized you could add an array_unique before looping since str_replace replaces all equal substrings.

$matches[0] = array_unique($matches[0]);

https://3v4l.org/mIiX8

Andreas
  • 23,610
  • 6
  • 30
  • 62
1

I created this simple function and it works like a charm

and you can add delimiters as you like.

function capitalize_after_delimiters($string='', $delimiters = array())
{
    foreach ($delimiters as $delimiter)
    {
        $temp = explode($delimiter, $string);
        array_walk($temp, function (&$value) { $value = ucfirst($value); });
        $string = implode($temp, $delimiter);
    }
    return $string;
}

$string ="I am a string with several periods.period #1. period #2.";

$result = capitalize_after_delimiters($string, array('.', '. '));

var_dump($result);

result: string(56) "I am a string with several periods.Period #1. Period #2."

result

Andreas
  • 23,610
  • 6
  • 30
  • 62
Sherif Salah
  • 2,085
  • 2
  • 9
  • 21
  • This solution is good but can fail in many ways. If the text has three spaces it fails (or you have to manually add it), if there is a new line after the dot it fails. https://3v4l.org/lcBZG – Andreas Nov 10 '18 at 19:55
  • I solved his problem as he asked and add some extra features too, but if you want to make it **absolutely perfect solution** in general, then we can work on that. – Sherif Salah Nov 10 '18 at 20:07
  • 1
    Serial downvoter in action, all answers has been downvoted at 18h05 UTC – Toto Nov 12 '18 at 19:28
0

If regex is not an option, something like this might work:

$str = "I am a string with several periods.period #1. period #2.";
$strings = explode('.', $str);
$titleCased = [];

foreach($strings as $s){
    $titleCased[] = ucfirst(trim($s));
}
echo join(".", $titleCased);

Although, this has the added effect of removing whitespace.

https://3v4l.org/fWGUW

Martin
  • 22,212
  • 11
  • 70
  • 132
erickb
  • 6,193
  • 4
  • 24
  • 19