Posted by : Safras Ahamed Monday, May 13, 2013

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

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Popular Post

Blogger templates

Labels

- Copyright © My Code Genius by 2 Geniuses -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -