Trang chủTrang chủ Liên hệLiên hệ
PHP BASIC

» Events  » References  » Tutorials  » Hướng dẫn  RSS Feed2.0 Đăng ký | Đăng nhập

Dịch PHP Manual (đã dịch 86 /1248)
Thêm bài viết mới
#Thêm ý kiến
Emotion|Show / Hide
Em có hàm cắt chuỗi có dấu unicode thế này.

function cstr($text, $start=0, $limit=12)
    {
        if (function_exists('mb_substr')) {
            $more = (mb_strlen($text) > $limit) ? TRUE : FALSE;
            $text = mb_substr($text, 0, $limit, 'UTF-8');
            return array($text, $more);
        } elseif (function_exists('iconv_substr')) {
            $more = (iconv_strlen($text) > $limit) ? TRUE : FALSE;
            $text = iconv_substr($text, 0, $limit, 'UTF-8');
            return array($text, $more);
        } else {
            preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $text, $ar);  
            if(func_num_args() >= 3) {  
                if (count($ar[0])>$limit) {
                    $more = TRUE;
                    $text = join("",array_slice($ar[0],0,$limit))."...";
                }
                $more = TRUE;
                $text = join("",array_slice($ar[0],0,$limit));
            } else {
                $more = FALSE;
                $text =  join("",array_slice($ar[0],0));
            }
            return array($text, $more);
        }
}

function cut_title($text, $limit=25)
{
    $val = cstr($text, 0, $limit);
    return $val[1] ? $val[0]."..." : $val[0];
}
$str = "Cộng hòa xã hội chủ nghĩa việt nam";
echo cut_title($str);
?>
Nhưng hàm này chỉ cho kết quả là một chuỗi, bây giờ mình muốn cắt làm 3 chuỗi con trong một chuỗi lớn chẳng hạn và các chuỗi con đó lần lượt xuống dòng.
Ví dụ em muốn cắt chuỗi trên thành 4 chuỗi thế này
Cộng hòa
xã hội
chủ nghĩa
việt nam
thì phải làm thế nào. Và trong chuỗi lớn em định cắt có cả mã html vì em dùng js editor mà. Các bác giúp em với.

Tác giả:hello

Show / Hide
#Thêm ý kiến

Add phpBasic to your browser's search box
Digg.com delicious Furl Google Bookmark Reddit Slashdot Yahoo My Web

Tableless template Framework PHP functions
 


XHTML Standard
Xây dựng & phát triển bởi tất cả các thành viên.
Powered by http://phpbasic.com