<?php
/**
* Get the position in a text inbetween two complete words just below
* a limit if the text is longer than the limit.
*
* @param unknown_type $text
* @param unknown_type $limit
* @return unknown
*/
function _get_complete_word($text, $limit = 5000) {
$text_lenght = strlen($text);
if ($text_lenght <= $limit) {
return $text_lenght;
}else{
preg_match_all("/ /ui", $text, $matches, PREG_OFFSET_CAPTURE);
foreach ($matches[0] as $key => $value) {
if ($value[1] >= $limit) {
return ($key == 0 ? $matches[0][$key][1] : $matches[0][$key-1][1]);
}
}
return $text_lenght;
}
}
?>