Skip to content

Instantly share code, notes, and snippets.

@lenivene
Last active January 24, 2020 05:33
Show Gist options
  • Save lenivene/bd373accd89c26f46fc6115e435f8be8 to your computer and use it in GitHub Desktop.
Save lenivene/bd373accd89c26f46fc6115e435f8be8 to your computer and use it in GitHub Desktop.
Function module to get first letter in text

How to use?

JavaScript

const getFirstLetter = require("./getFirstLetter");
const result = getFirstLetter("Lenivene Bezerra");

console.log(result); // LB

PHP

<?php
require_once './getFirstLetter.php';

if( function_exists( 'getFirstLetter' ) ){
echo getFirstLetter( 'Lenivene Bezerra' ); // LB
}
?>
module.exports = function getFirstLetter(text) {
let acronym = "";
const words = text.split(" ");
words.map(word => {
acronym += word[0];
});
return acronym;
};
<?php
function getFirstLetter($text) {
$acronym = "";
$words = explode(" ", $text);
foreach( $words as $word ){
$acronym .= $word[0];
}
return $acronym;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment