function create_safe_string($string, $separator = META_DEFAULT_WORDS_SEPARATOR, $word_length = META_DEFAULT_WORD_LENGTH) { if (empty($separator)) { $separator = ' '; } $string = trim(strip_tags($string)); $string = trim(preg_replace('#[^\\p{L}\\p{N}]+#u', ' ', $string)); $string = preg_replace('/\\s\\s+/', ' ', $string); if ($word_length > 1) { $words_array = explode($separator, $string); if (is_array($words_array)) { for ($i = 0, $j = count($words_array); $i < $j; $i++) { $char_size = tep_utf8_size($words_array[$i]); $word_size = $word_length * $char_size; if (strlen($words_array[$i]) < $word_size) { unset($words_array[$i]); } } if (count($words_array)) { $string = implode($separator, $words_array); } } } return $string; }
function create_safe_description($string, $max_length = META_MAX_DESCRIPTION, $open_tag = '<p>', $close_tag = '</p>') { $string = trim(strip_tags($string, $open_tag)); $open_pos = strpos($string, $open_tag); $close_pos = strpos($string, $close_tag); if ($open_pos !== false && $close_pos !== false && $close_pos > $open_pos) { $open_pos += strlen($open_tag); //$close_pos -= strlen($close_tag); $final_string = substr($string, $open_pos, $close_pos - $open_pos); } else { $final_string = strip_tags($string); } $char_size = tep_utf8_size($final_string); $max_length *= $char_size; if (strlen($final_string) > $max_length) { $final_string = substr($final_string, 0, $max_length); } return $final_string; }
function tep_string_length($string) { $size = tep_utf8_size($string); $length = (int) (strlen($string) / $size); return $length; }