定义:一个字符串 string 就是由一系列的字符组成,其中每个字符等同于一个字节。这意味着 PHP 只能支持 256 的字符集,因此不支持 Unicode 。相信很多新手在官网看到这句话的时候都会觉得很疑惑,明明PHP不是可以打印出中文吗?为什么会说字符串不支持Unicode呢?实际上,这句话应该这样理解:字符串会原生支持ascii字符集,会直接保存不进行编码,但是对于其他字符集会自动根据文档的编码来进行编码,所以如果文档的编码设置正确的话,是可以正确将保存在字符串里面的编码正确读取出来的。

常用函数
因为字符串的函数非常多,我粗略的数了一下,接近100个,所以想要都记住是不太现实的,只能把常用记住,其他需要用到的时候再来看手册吧。


连接与分割

1.explode — 使用一个字符串分割另一个字符串

用法:

array explode ( string $delimiter , string $string [, int $limit ] )

示例:

$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

2.implode — 将一个一维数组的值转化为字符串

用法:

string implode ( string $glue , array $pieces )

示例:

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone

3.str_split — 将字符串转换为数组

用法:

array str_split ( string $string [, int $split_length = 1 ] )

示例:

$str = "Hello Friend";

$arr1 = str_split($str);
$arr2 = str_split($str, 3);

print_r($arr1);
print_r($arr2);

//输出的结果
Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>
    [6] => F
    [7] => r
    [8] => i
    [9] => e
    [10] => n
    [11] => d
)

Array
(
    [0] => Hel
    [1] => lo
    [2] => Fri
    [3] => end
)

4.mb_split — 使用正则表达式分割多字节字符串

用法:

array mb_split ( string $pattern , string $string [, int $limit = -1 ] )

示例:

To split an string like this: "日、に、本、ほん、語、ご" using the "、" delimiter i used:

     $v = mb_split('、',"日、に、本、ほん、語、ご");

but didn't work.

The solution was to set this before:

       mb_regex_encoding('UTF-8');
      mb_internal_encoding("UTF-8"); 
     $v = mb_split('、',"日、に、本、ほん、語、ご");

and now it's working:

Array
(
    [0] => 日
    [1] => に
    [2] => 本
    [3] => ほん
    [4] => 語
    [5] => ご
)

5.preg_split — 通过一个正则表达式分隔字符串

用法:

array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )

示例:

//使用逗号或空格(包含" ", \r, \t, \n, \f)分隔短语
$keywords = preg_split("/[\s,]+/", "hypertext language, programming");
print_r($keywords);

//输出
Array
(
    [0] => hypertext
    [1] => language
    [2] => programming
)

6.strstr — 查找字符串的首次出现,其忽略大小写版本为stristr

用法:

string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )

示例:

$email  = 'name@example.com';
$domain = strstr($email, '@');
echo $domain; // 打印 @example.com

$user = strstr($email, '@', true); // 从 PHP 5.3.0 起
echo $user; // 打印 name

7.strrchr — 查找指定字符在字符串中的最后一次出现

用法:

string strrchr ( string $haystack , mixed $needle )

示例:

$path = '/www/public_html/index.html';
$filename = substr(strrchr($path, "/"), 1);
echo $filename; // "index.html"

8.strpos — 查找字符串首次出现的位置,对应的最后出现的函数为strrpos

用法:

mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

9.substr — 返回字符串的子串

用法:

string substr ( string $string , int $start [, int $length ] )

示例:

$rest = substr("abcdef", -1);    // 返回 "f"
$rest = substr("abcdef", -2);    // 返回 "ef"
$rest = substr("abcdef", -3, 1); // 返回 "d"

查找与替换

1.preg_match — 执行一个正则表达式匹配

用法:

int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )

示例:

//从URL中获取主机名称
preg_match('@^(?:http://)?([^/]+)@i',
    "http://www.php.net/index.html", $matches);
$host = $matches[1];//$host为www.php.net

//获取主机名称的后面两部分
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}\n";
//输出domain name is: php.net


Regex quick reference
[abc]     A single character: a, b or c
[^abc]     Any single character but a, b, or c
[a-z]     Any single character in the range a-z
[a-zA-Z]     Any single character in the range a-z or A-Z
^     Start of line
$     End of line
\A     Start of string
\z     End of string
.     Any single character
\s     Any whitespace character
\S     Any non-whitespace character
\d     Any digit
\D     Any non-digit
\w     Any word character (letter, number, underscore)
\W     Any non-word character
\b     Any word boundary character
(...)     Capture everything enclosed
(a|b)     a or b
a?     Zero or one of a
a*     Zero or more of a
a+     One or more of a
a{3}     Exactly 3 of a
a{3,}     3 or more of a
a{3,6}     Between 3 and 6 of a

options: i case insensitive m make dot match newlines x ignore whitespace in regex o perform #{...} substitutions only once

2.preg_replace — 执行一个正则表达式的搜索和替换,preg_filter()等价于preg_replace() 除了它仅仅返回(可能经过转化)与目标匹配的结果

用法:

mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

示例:

$string = 'The quick brown fox jumped over the lazy dog.';
$patterns = array();
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements = array();
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);

//输出The bear black slow jumped over the lazy dog.

3.wordwrap — 打断字符串为指定数量的字串

用法:

string wordwrap ( string $str [, int $width = 75 [, string $break = "\n" [, bool $cut = false ]]] )

示例:

$text = "A very long woooooooooooord.";
$newtext = wordwrap($text, 8, "\n", true);

echo "$newtext\n";

//输出
A very
long
wooooooo
ooooord.

4.nl2br — 在字符串所有新行之前插入 HTML 换行标记

用法:

string nl2br ( string $string [, bool $is_xhtml = true ] )

5.htmlspecialchars — Convert special characters to HTML entities htmlspecialchars_decode — 将特殊的 HTML 实体转换回普通字符

用法:

string htmlspecialchars ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = true ]]] )

6.htmlentities — Convert all applicable characters to HTML entities

用法:

string htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = true ]]] )

示例:

$str = "A 'quote' is <b>bold</b>";

// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str);
//和htmlspecialchars的输出一样

// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str, ENT_QUOTES);

加密

1.md5 — 计算字符串的 MD5 散列值,md5_file — 计算指定文件的 MD5 散列值

用法:

string md5 ( string $str [, bool $raw_output = false ] )

2.sha1 — 计算字符串的 sha1 散列值,sha1_file — 计算文件的 sha1 散列值

用法:

string sha1 ( string $str [, bool $raw_output = false ] )

3.hash — 生成哈希值 (消息摘要),hash_file — 使用给定文件的内容生成哈希值

用法:

string hash ( string $algo , string $data [, bool $raw_output = false ] )

4.crc32 — 计算一个字符串的 crc32 多项式

用法:

int crc32 ( string $str )

生成 str 的 32 位循环冗余校验码多项式。这通常用于检查传输的数据是否完整。


其他

1.addslashes — 使用反斜线引用字符串

用法:

string addslashes ( string $str )

2.lcfirst — 使一个字符串的第一个字符小写,ucfirst — 将字符串的首字母转换为大写

用法:

string lcfirst ( string $str )

3.ucwords — 将字符串中每个单词的首字母转换为大写

用法:

string ucwords ( string $str )

4.strtoupper — 将字符串转化为大写,strtolower — 将字符串转化为小写

用法:

string strtoupper ( string $string )

5.trim — 去除字符串首尾处的空白字符(或者其他字符),ltrim删除字符串开头的空白字符(或其他字符),rtrim删除字符串末端的空白字符(或者其他字符)

用法:

string trim ( string $str [, string $charlist = " \t\n\r\0\x0B" ] )

6.sprintf — 返回一个格式化的字符串

用法:

string sprintf ( string $format [, mixed $args [, mixed $... ]] )
//Possible format values:
%% - Returns a percent sign
%b - Binary number
%c - The character according to the ASCII value
%d - Signed decimal number (negative, zero or positive)
%e - Scientific notation using a lowercase (e.g. 1.2e+2)
%E - Scientific notation using a uppercase (e.g. 1.2E+2)
%u - Unsigned decimal number (equal to or greather than zero)
%f - Floating-point number (local settings aware)
%F - Floating-point number (not local settings aware)
%g - shorter of %e and %f
%G - shorter of %E and %f
%o - Octal number
%s - String
%x - Hexadecimal number (lowercase letters)
%X - Hexadecimal number (uppercase letters)

//Additional format values. These are placed between the % and the letter (example %.2f):

+ (Forces both + and - in front of numbers. By default, only negative numbers are marked)
' (Specifies what to use as padding. Default is space. Must be used together with the width specifier. Example: %'x20s (this uses "x" as padding)
- (Left-justifies the variable value)
[0-9] (Specifies the minimum width held of to the variable value)
.[0-9] (Specifies the number of decimal digits or maximum string length)

7.strcmp — 二进制安全字符串比较,strcasecmp — 二进制安全比较字符串(不区分大小写)

用法:

int strcmp ( string $str1 , string $str2 )

8.strrev — 反转字符串

用法:

string strrev ( string $string )

参考网站


字符串

登陆发表评论