Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $str = 'hello kitty';
- $pos = strpos($str, 'kit');
- $word = extractWordAtPosition($str, $pos);
- var_dump($word);
- function extractWordAtPosition($str, $position)
- {
- if ($position === false || !$str) {
- return '';
- }
- $words = explode(' ', $str);
- $characters = -1;
- foreach($words as $word){
- $characters += strlen($word);
- if($characters >= $position){
- return $word;
- }
- }
- return '';
- }
Add Comment
Please, Sign In to add comment