0

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.

Thanks

4
Contributors
8
Replies
26
Views
4 Years
Discussion Span
Last Post by minitauros
1

Great ^^. How did you do it?

What I'd do is:

<?php
$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));

Edited by minitauros

0

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.

Edited by Octet

0

oke here

<?php

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

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


                    ?>

Edited by johnnyd1963

0

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.

0

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!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.