Monday, May 13, 2013

Getting the first letters of text using PHP [SafGetFirstLetters]

It is always better to make small small functions and keep a library, I was collecting all my experimental functions since I am in to PHP.

I wanted to take first letters from a line of text and realized that I have to use this for many places. Yesterday I have made a new one "SafGetFirstLetters"?

What does it do?
Example Text: This Is A Example Text Line
Output: TTIAETL

SafGetFirstLetters("Lagoon Water Villa with Private Pool", 5)
will return: "LWVwP"
5 is the limit of the text I have asked from function to return.

function SafGetFirstLetters($Text,$MaxChr){
 $TextArray = explode(" ",$Text);
 $WordCount = count($TextArray);
 if ($WordCount) {
  $MakeText='';
  for ($i_a = 0; $i_a < $WordCount; $i_a++) {
   $MakeText = $MakeText . substr($TextArray[$i_a],0,1);
  }
 }
 if ($MaxChr) {
  return substr($MakeText,0,$MaxChr);
 }else{
  return $MakeText;
 }
}
If you like it please comment.
Thanks.
Safras Ahamed

No comments:

Post a Comment