Alex,
Under what conditions would you need to truncate content with tags
included?
Here is my function to truncate (called 'cut'). Note that it strips
the tags.
function cut ($string, $length, $padd = '...') {
$string = strip_tags($string);
$returnString = '';
if (strlen($string) <= $length)
{
$returnString = $string;
}
else
{
$returnString = substr($string, 0,
$length-strlen($padd)).$padd;
}
return $returnString;
}
Hope this helps.
Dimitry Z.
On Feb 14, 1:01 pm, "Alex" <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm always wondering if there is a truncate function for a Text (a
> post f.e.) which considers opened HTML tags. Everytime i'd truncated a
> text with the normal truncate function available in TextHelper, some
> html tags werent closed and so my whole page looks damaged.
> So i programmed a function which closes HTML functions.
> I like you to review this function and to tell me your ideas.
> Up to now, the function only acepts std xhtml:
>
> function truncate($text, $length, $ending = '...', $exact = false) {
> if(strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
> return $text;
> }
>
> preg_match_all('/(<.+?>)?([^<>]*)/is', $text, $ausgabe,
> PREG_SET_ORDER);
>
> $total_length = 0;
> $arr_elements = array();
> $truncate = '';
>
> foreach($ausgabe as $treffer) {
> if(!empty($treffer[1])) {
> //<img />
> if(preg_match('/^<\s*.+?\/\s*>$/s', $treffer[1])) {
> //echo
> '1:'.htmlspecialchars($treffer[1]).'<br>';
> //</b>
> } else if(preg_match('/^<\s*\/([^\s]+?)\s*>$/s',
> $treffer[1], $treffer2)) {
> //echo '2:'.htmlspecialchars($treffer2[1]).'<br>';
> $pos = array_search($treffer2[1], $arr_elements);
> if($pos !== false) {
> unset($arr_elements[$pos]);
> }
> //<b>
> } else if(preg_match('/^<\s*([^\s>!]+).*?>$/s',
> $treffer[1], $treffer2)) {
> array_unshift($arr_elements,
> strtolower($treffer2[1]));
> //echo '3:'.htmlspecialchars($treffer2[1]).'<br>';
> }
> $truncate .= $treffer[1];
> }
> $content_length =
> strlen(preg_replace('/(&[a-z]{1,6};|&#[0-9]+;)/
> i', ' ', $treffer[2]));
> if($total_length >= $length) {
> break;
> } else if($total_length+$content_length > $length) {
> $left =
> $total_length>$length?$total_length-$length:$length-
> $total_length;
> $entities_length = 0;
>
> if(preg_match_all('/&[a-z]{1,6};|&#[0-9]+;/i', $treffer[2],
> $treffer3, PREG_OFFSET_CAPTURE)) {
> foreach($treffer3[0] as $entity) {
> if($entity[1]+1-$entities_length <= $left) {
> $left--;
> $entities_length += strlen($entity[0]);
> } else break;
> }
> }
> $truncate .= substr($treffer[2], 0,
> $left+$entities_length);
> break;
> } else {
> $truncate .= $treffer[2];
> $total_length += $content_length;
> }
> }
> //var_dump($ausgabe);
> if(!$exact) {
> $spacepos = strrpos($truncate, ' ');
> if(isset($spacepos)) {
> $truncate = substr($truncate, 0, $spacepos);
> }
> }
> $truncate .= $ending;
> foreach($arr_elements as $element) {
> $truncate .= '</' . $element . '>';
> }
> return $truncate;
> }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---