0

I have the following paragraph :

This is the first line.
this is the second line.
we live in Europe.
this is the fourth line.

I want to convert the first character to uppercase in every newlines.

So the paragraph should look like this :

This is the first line.
This is the second line.
We live in Europe.
This is the fourth line.

So far, I am able to convert the first character to uppercase, but it converts first characters in every words not in newlines using the ucfirst() and ucword()

echo ucfirst($str);

Is there a way to solve this using ucfirst() or preg_replace() function ?

Thanks!

user4679529
  • 341
  • 3
  • 10
Amit Verma
  • 40,709
  • 21
  • 93
  • 115

4 Answers4

5

How about this one?

$a = "This is the first line.\n\r
this is the second line.\n\r
we live in Europe.\n\r
this is the fourth line.";

$a = implode("\n", array_map("ucfirst", explode("\n", $a)));
invisal
  • 11,075
  • 4
  • 33
  • 54
3

You could use this.

<?php
$str = strtolower('This is the first line.
This is the second line.
We live in Europe.
This is the fourth line.');
$str = preg_replace_callback('~^\s*([a-z])~im', function($matches) { return strtoupper($matches[1]); }, $str);
echo $str;

Output:

This is the first line.
This is the second line.
We live in europe.
This is the fourth line.

The i modifier says we don't care about the case and the m says every line of the string is a new line for the ^. This will capitalize the first letter of a line presuming it starts with an a-z.

chris85
  • 23,846
  • 7
  • 34
  • 51
  • `\i` modifier is not required there as you have already lower-cased the input. Besides we need to match and replace lower cased char only so its not required at all. – Ulver May 22 '15 at 02:42
  • 1
    This discards any non-line-starting capitalization, though, which would not usually be appropriate. You can see the proper noun Europe was lower-cased, as would the pronoun I. – Chris Baker May 22 '15 at 02:53
  • Can you phrase that differently @ChrisBaker, or provide an example not sure I'm following. Ulver I noted what the modifier does OP can use it or not. The lower casing I'm doing is to illustrate the usage. – chris85 May 22 '15 at 03:04
  • Here an example: Output here: `We live in europe.` Expected output: `We live in Europe.` – Ulver May 22 '15 at 03:08
  • Ah, yes I see what you mean now. The OP shouldn't run their string through the `strtolower`. This is just a regex example of how this could be accomplished. – chris85 May 22 '15 at 05:53
1

Another way to do it is:

    <?php

   function foo($paragraph){

        $parts = explode("\n", $paragraph);
        foreach ($parts as $key => $line) {  
            $line[0] = strtoupper($line[0]);
            $parts[$key] = $line;
        }

        return implode("\n", $parts);
    }

    $paragraph = "This is the first line.
    this is the second line.
    we live in Europe.
    this is the fourth line.";

    echo foo($paragraph);
?>
Emiliano Sangoi
  • 921
  • 10
  • 20
1

Replace the very first lower cased char of every line by upper case:

$str = preg_replace_callback(
            '/^\s*([a-z])/m',
            function($match) {
                return strtoupper($match[1]);
            },
            $str);
Ulver
  • 905
  • 8
  • 13