bueltge
11/13/2011 - 8:44 PM

Extract word from a string given a position in php

Extract word from a string given a position in php

function extractWord($text, $position){
   $words = explode(' ', $text);
   $characters = -1;
   foreach($words as $word){
      $characters += strlen($word);
      if($characters >= $position){
         return $word;
      }
   }
   return '';
}