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
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
sorry i got the answer
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));
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
<?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;
?>
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!
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.