(PHP 4 >= 4.0.6, PHP 5)
mb_substr — 获取字符串的部分
$str
, int $start
[, int $length
[, string $encoding
]] )
根据字符数执行一个多字节安全的 substr() 操作。
位置是从 str
的开始位置进行计数。
第一个字符的位置是 0。第二个字符的位置是 1,以此类推。
str
从该 string 中提取子字符串。
start
str
中要使用的第一个字符的位置。
length
str
中要使用的最大字符数。
encoding
encoding
参数为字符编码。如果省略,则使用内部字符编码。
mb_substr() 函数根据 start
和 length
参数返回 str
中指定的部分。
desmatic at gmail dot com (2013-04-29 15:51:04)
quick and dirty loop through multibyte string
<?php
function get_character_classes($string, $encoding = "UTF-8") {
$current_encoding = mb_internal_encoding();
mb_internal_encoding($encoding);
$has = array();
$stringlength = mb_strlen($string, $encoding);
for ($i=0; $i < $stringlength; $i++) {
$c = mb_substr($string, $i, 1);
if (($c >= "0") && ($c <= "9")) {
$has['numeric'] = "numeric";
} else if (($c >= "a") && ($c <= "z")) {
$has['alpha'] = "alpha";
$has['alphalower'] = 'alphalower';
} else if (($c >= "A") && ($c <= "Z")) {
$has['alpha'] = "alpha";
$has['alphaupper'] = "alphaupper";
} else if (($c == "$") || ($c == "?")) {
$has['currency'] = "currency";
} else if (($c == ".") && ($has['decimal'])) {
$has['decimals'] = "decimals";
} else if ($c == ".") {
$has['decimal'] = "decimal";
} else if ($c == ",") {
$has['comma'] = "comma";
} else if ($c == "-") {
$has['dash'] = "dash";
} else if ($c == " ") {
$has['space'] = "space";
} else if ($c == "/") {
$has['slash'] = "slash";
} else if ($c == ":") {
$has['colon'] = "colon";
} else if (($c >= " ") && ($c <= "~")) {
$has['ascii'] = "ascii";
} else {
$has['binary'] = "binary";
}
}
mb_internal_encoding($current_encoding);
return $has;
}
$string = "1234asdfA?^_{}|}~????";
echo print_r(get_character_classes($string), true);
?>
Array
(
[numeric] => numeric
[alpha] => alpha
[alphalower] => alphalower
[alphaupper] => alphaupper
[currency] => currency
[ascii] => ascii
[binary] => binary
)
qeremy [atta] gmail [dotta] com (2012-02-27 15:58:54)
A proper (logical) alternative for unicode strings;
<?php
function substr_unicode($str, $s, $l = null) {
return join("", array_slice(
preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY), $s, $l));
}
$str = "Büyük";
$s = 0; // start from "0" (nth) char
$l = 3; // get "3" chars
echo substr($str, $s, $l) ."\n"; // Bü
echo mb_substr($str, $s, $l) ."\n"; // Bü
echo substr_unicode($str, $s, $l); // Büy
?>
p dot assenov at aip-solutions dot com (2011-12-02 09:17:20)
I'm trying to capitalize only the first character of the string and tried some of the examples above but they didn't work. It seems mb_substr() cannot calculate the length of the string in multi-byte encoding (UTF-8) and it should be set explicitly. Here is the corrected version:
<?php
function mb_ucfirst($str, $enc = 'utf-8') {
return mb_strtoupper(mb_substr($str, 0, 1, $enc), $enc).mb_substr($str, 1, mb_strlen($str, $enc), $enc);
}
?>
cheers!
levani9191 at gmail dot com (2010-07-18 03:37:30)
A simple code that check if the latest symbol in the string is a question mark and adds one if it doesn't...
<?php $string = (mb_substr($string, -1, 1, 'UTF-8') != '?') ? $string.'?' : $string; ?>
Anonymous (2010-02-26 05:15:46)
If start is negative, the returned string will start at the start'th character from the end of string
dziamid at gmail dot com (2009-02-06 08:27:17)
Here is my solution to highlighting search queries in multibyte text:
<?php
function mb_highlight($data, $query, $ins_before, $ins_after)
{
$result = '';
while (($poz = mb_strpos(mb_strtolower($data), mb_strtolower($query))) !== false)
{
$query_len = mb_strlen ($query);
$result .= mb_substr ($data, 0, $poz).
$ins_before.
mb_substr ($data, $poz, $query_len).
$ins_after;
$data = mb_substr ($data, $poz+$query_len);
}
return $result;
}
?>
Enjoy!
[EDIT BY danbrown AT php DOT net: Reclassified to a more appropriate function manual page.]
projektas at gmail dot com (2008-10-21 06:29:40)
First letter in upper case <hr />
<?php
header ('Content-type: text/html; charset=utf-8');
if (isset($_POST['check']) && !empty($_POST['check'])) {
echo htmlspecialchars(ucfirst_utf8($_POST['check']));
} else {
echo htmlspecialchars(ucfirst_utf8('??sin?'));
}
function ucfirst_utf8($str) {
if (mb_check_encoding($str,'UTF-8')) {
$first = mb_substr(
mb_strtoupper($str, "utf-8"),0,1,'utf-8'
);
return $first.mb_substr(
mb_strtolower($str,"utf-8"),1,mb_strlen($str),'utf-8'
);
} else {
return $str;
}
}
?>
<form method="post" action="" >
<input type="input" name="check" />
<input type="submit" />
</form>
Silvan (2007-09-01 15:30:26)
Passing null as length will not make mb_substr use it's default, instead it will interpret it as 0.
<?php
mb_substr($str,$start,null,$encoding); //Returns '' (empty string) just like substr()
?>
Instead use:
<?php
mb_substr($str,$start,mb_strlen($str),$encoding);
?>
xiaogil at yahoo dot fr (2005-08-02 08:33:36)
Thanks Darien from /freenode #php for the following example (a little bit changed).
It just prints the 6th character of $string.
You can replace the digits by the same in japanese, chinese or whatever language to make a test, it works perfect.
<?php
mb_internal_encoding("UTF-8");
$string = "0123456789";
$mystring = mb_substr($string,5,1);
echo $mystring;
?>
(I couldn't replace 0123456789 by chinese numbers for example here, because it's automatically converted into latin digits on this website, look :
零一二三四
五六七八九)
gilv
drraf at tlen dot pl (2005-02-23 06:44:00)
Note: If borders are out of string - mb_string() returns empty _string_, when function substr() returns _boolean_ false in this case.
Keep this in mind when using "===" comparisions.
Example code:
<?php
var_dump( substr( 'abc', 5, 2 ) ); // returns "false"
var_dump( mb_substr( 'abc', 5, 2 ) ); // returns ""
?>
It's especially confusing when using mbstring with function overloading turned on.