Search code examples
phpcapitalize

Capitalize first letter of each line using PHP


I'd like if there was a way to capitalize the first letter of each line using PHP.

<?php 
$text="namjaga da geureochi mwo narago dareugetni
cheoeumen da akkyeojwodo naragabeorigo maneun
namjaga da geureochi mwo narago teukbyeolhalkka
ni mameul da gajyeodo naragabeorigo maneun
namjaga da geureochi mwo"
?>

<p>
<?php echo $text; ?>
</p>

Thank you for your help !


Solution

  • You can try something like

    $textParts = explode("\n", $text);
    foreach ($textParts as $key => $value) {
       $textParts[$key] = ucfirst($value);
    }
    
    $text = implode("\n", $textParts);