downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

mb_strimwidth> <mb_split
Last updated: Fri, 14 Aug 2009

view this page in

mb_strcut

(PHP 4 >= 4.0.6, PHP 5)

mb_strcutCoupe une partie de chaîne

Description

string mb_strcut ( string $str , int $start [, int $length [, string $encoding ]] )

mb_strcut() effectue une opération équivalente à mb_substr() avec des méthodes différentes. Si la position start est un caractère multi-octets ou plus, il débutera à partir du premier octet ou du premier caractère multi-octets.

Il soustrait la chaîne str qui est plus courte que length ET caractères qui ne font pas partis d'une chaîne multi-octets et qui ne commence pas au milieu de la séquence.

Liste de paramètres

str

La chaîne à couper.

start

La position à partir de laquelle on commence à couper.

length

Le nombre d'octets à couper.

encoding

Le paramètre encoding est l'encodage des caractères. S'il est omis, l'encodage de caractres interne sera utilisé.

Valeurs de retour

mb_strcut() retourne la portion de la chaîne str qui commence au caractère start et a la longueur de length caractères.

Voir aussi



mb_strimwidth> <mb_split
Last updated: Fri, 14 Aug 2009
 
add a note add a note User Contributed Notes
mb_strcut
egoalesum at IHATEBOTS dot youarchive dot it
21-May-2009 06:07
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.
t dot starling at physics dot unimelb dot edu dot au
27-Aug-2004 01:01
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
26-Sep-2003 12:53
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

mb_strimwidth> <mb_split
Last updated: Fri, 14 Aug 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites