用ascii符号截断

时间:2016-12-02 14:38:51

标签: php split truncate

我遇到截断php函数的问题..

<?php

print_r(truncate ('cia???☺☻♥♀♂☼•◘○♠♣xas?????!!!!----'));

function truncate($text) {
    $length = 100; 
    $ending = '...';
    $exact = true;
    $considerHtml = false; 
    $stripTags = false;
    $wordsLenght = 20;


    $textArray = explode ( " ", $text );

    foreach ( $textArray as $key => $word ) {
        if (strlen ( $word ) > $wordsLenght) {
            $truncatedWord = substr ( $word, 0, $wordsLenght );
            $textArray [$key] = $truncatedWord . "[...]";
        }
    }

    $text = implode ( " ", $textArray );
    // end truncate long word

    if (strlen ( $text ) <= $length) {
        return $text;
    } else {
        $truncate = substr ( $text, 0, $length - mb_strlen ( $ending, 'UTF-8' ) );
    }
}

// if the words shouldn't be cut in the middle...
if (! $exact) {
    // ...search the last occurance of a space...
    $spacepos = strrpos ( $truncate, ' ' );

    if (isset ( $spacepos )) {
        // ...and cut the text in this position
        $truncate = substr ( $truncate, 0, $spacepos );
    }
}

// add the defined ending to the text
$truncate .= $ending;

if ($considerHtml) {
    // close all unclosed html-tags
    foreach ( $open_tags as $tag ) {
        $truncate .= '';
    }
}

return $truncate;

问题,就是给出的字符串,就是截断函数对unicode符号不起作用......

结果如下:

  

CIA ???☺☻♥♀[...]

有没有办法正确分割?

我尝试过不同的方式,但没有一个能正常工作......我不在乎:)

0 个答案:

没有答案