Hi hope someone can help me with the folowing

How can i echo only the first letter of given words

Example if i have: Jozef Margaretha Fransiscus

i Want: J.M.F.


Great ^^. How did you do it?

What I'd do is:

$parts = explode(' ', $name);

for($i=0; $i<count($parts); $i++)
    $first_letter = $parts[$i][0]; // Gets the first letter of the given part.
    $letters[] = $first_letter;

$initials_only = implode('.', array_map('strtoupper', $letters));

also check str_split() which will turn a word into array.

And also substr($Word[0], 0); which shall return letter 0 (first one) from an array of words. You can then iterate through the array and store it in whatever variable, or output directly to the screen as you please.

oke here


                    $string = "Jozef Margaretha Franciscus";
                    $word = strtoupper($string);
                    $words = explode(" ", $word);
                    $doopnamen = " ";
                    foreach($words as $value) { $doopnamen .= substr($value, 0, 1).'.'; }

                    echo $word;
                    echo "<br/>";
                    echo $doopnamen;


Well yea that's pretty much what I was thinking of :). Just a tip for your knowledge:

If you have a string, substr($string, 0, 1) does the same as $string[0]. Using $string[number] you can select any letter in a string.

I know but i need only first letters.

I know! Therefore in your line

foreach($words as $value) { $doopnamen .= substr($value, 0, 1).'.'; }

The substr($value, 0, 1) could be replaced by $value[0].

If you know what I mean :). Anyway, good luck with your project!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.