function cut_sense($matne_harf, $l_harf ,$return=1 ) {
if ( strlen($matne_harf) > $l_harf){
$end='...';
}else{
$end='';
}
if ( function_exists('mb_strcut') ){
$matne_harf = mb_strcut ( $matne_harf, 0 , $l_harf , "UTF-8" );
}else{
$matne_harf =substr($matne_harf, 0, $l_harf);
}
$text=''.$matne_harf.''.$end.'';
if ( $return == 1){
return $text;
}else{
print $text;
}
}
Iranian php programmer (farhad zand +989383015266)
mb_strcut
(PHP 4 >= 4.0.6, PHP 5)
mb_strcut — 文字列の一部を得る
説明
string mb_strcut
( string
$str
, int $start
[, int $length
[, string $encoding
]] )mb_strcut() は、ある文字列からの部分文字列の抽出を mb_substr() と同じ方法で行います。ただし、 処理は文字単位ではなくバイト単位で行います。 切り出し位置がたまたまマルチバイト文字の 2 バイト目以降だった場合、 切り出しはその文字の最初のバイトから行われます。この挙動もまた substr() 関数と異なるところです。 substr() の場合は、 マルチバイト文字の 2 バイト目以降であってもその位置から切り出しを行い、 結果的に壊れたバイト列を返すことになります。
パラメータ
-
str -
取り出しの対象となる文字列。
-
start -
バイト単位での開始位置。
-
length -
バイト単位での長さ。
-
encoding -
encodingパラメータには文字エンコーディングを指定します。省略した場合は、 内部文字エンコーディングを使用します。
返り値
mb_strcut() は、
start および
length パラメータで指定した
str の一部を返します。
php_engineer_bk at yahoo dot com ¶
2 years ago
t dot starling at physics dot unimelb dot edu dot au ¶
8 years ago
What the manual and the first commenter are trying to say is that mb_strcut uses byte offsets, as opposed to mb_substr which uses character offsets.
Both mb_strcut and mb_substr appear to treat negative and out-of-range offsets and lengths in the basically the same way as substr. An exception is that if start is too large, an empty string will be returned rather than FALSE. Testing indicates that mb_strcut first works out start and end byte offsets, then moves each offset left to the nearest character boundary.
oyag02 at yahoo dot co dot jp ¶
9 years ago
diffrence between mb_substr and mb_substr
example:
mb_strcut('I_ROHA', 1, 2) returns 'I_'. Treated as byte stream.
mb_substr('I_ROHA', 1, 2) returns 'ROHA' Treated as character stream.
# 'I_' 'RO' 'HA' means multi-byte character
egoalesum at IHATEBOTS dot youarchive dot it ¶
4 years ago
I found this function to be extremely useful.
Here is a practical example, showing the difference between substr(), mb_substr() and mb_strcut():
<?php
mb_internal_encoding('UTF-8');
$string = 'cioèòà';
var_dump(
substr($string, 0, 6),
mb_substr($string, 0, 6),
mb_strcut($string, 0, 6)
);
?>
Output:
string(6) "cioè?"
string(9) "cioèòà"
string(5) "cioè"
Explanation:
$string is long 9 bytes
c - 1 byte
i - 1 byte
o - 1 byte
è - 2 bytes
ò - 2 bytes
à - 2 bytes
substr() works with bytes, so it returns a string which is exactly 6 bytes long. Thus, it truncates the ò character.
mb_substr(), instead, works with characters, so it returns a string which is exactly 6 characters long (but in this case is 9 bytes long).
mb_strcut() works exactly as substr(), but, if the last byte appears to be truncated, it simply omits the character.
When you use
$string = mb_strcut($string, 6);
you can know for sure that strlen($string) <= 6. But no unicode characters will be truncated.
I hope my comment could finally be a simple explanation.
