/ Published in: PHP
Released into the public domain. Always supply $position as the index of a character within $text that is not a space.
Expand |
Embed | Plain Text
function edge_of_word($text, $position, $left = false) { if(!$left&&$first_space!==false&&$position<$first_space) $position = $first_space; if($last_space!==false&&$position>$last_space) while($text[$position]!=' '&&((!$left&&$position<strlen($text))||($left&&$position>0))) { $position += $left ? -1 : 1; } } return $position; } function find_adjacent_words($text, $position, $word_count, $left, $positions_only = false) { if($left===3) { $adjacent_words[] = find_adjacent_words($text, $position, $word_count, true, $positions_only); $adjacent_words[] = find_adjacent_words($text, $position, $word_count, false, $positions_only); return $adjacent_words; } else { $position = edge_of_word($text, $position, $left); $original_position = $position; $current_word_count = 0; while((($left&&$position>0)||(!$left&&$position<strlen($text)))&&$current_word_count<=$word_count) { if($text[$position]==' ') $current_word_count++; $position += $left ? -1 : 1; } $start_position = $left ? $position == 0 && $text[0] != ' ' ? $position : $position + 1 : $original_position; $length_of_words = $left ? $original_position - $position : $position - $original_position; } }
You need to login to post a comment.
