Capitalize first letters of a string with php

Question

How can I make the first letters of each word in a string uppercase in PHP?

Answer ( 1 )

    0
    2023-01-26T16:37:22+00:00

    You can use the ucwords() function which is a built-in PHP function that converts the first character of each word in a string to uppercase and the rest to lowercase. It’s similar to ucfirst(), which only capitalizes the first character of the first word in a string.

    function capitalizeWords($string) {
        return ucwords($string);
    }

    Then use the function like this:

    $string = "hello world";
    $capitalizedString = capitalizeWords($string);
    echo $capitalizedString; // Outputs: "Hello World"

Leave an answer