您的位置:首页 > 编程语言 > PHP开发

php开发中常用字符串函数总结

2011-07-27 09:16 696 查看
1. strlen()

计算字符串长度

2. substr

截取字符串,采用 起点—长度方式。
//特例需要记住 取字符串除了最后一位的所有字符
$rest = substr("abcdef", 0, -1); // returns "abcde"

3. substr_replace 查找替换

string substr_replace ( string string, string replacement, int start [, int length])
第一个参数是原串,第二个参数是替换内容,第三个参数替换开始位置 第四个参数为结束位置
<?PHP
$var = 'ABCDEFGH:/MNRPQR/';
echo "Original: $var<hr />\n";
/*第一个参数是原串,第二个参数是替换内容,第三个参数替换开始位置 第四个参数为结束位置
//函数作用在$var中用 bob 替换 给定范围的内容,第三个参数为开始位置 第四个参数为结束位置
/* These two examples replace all of $var with 'bob'. */
echo substr_replace($var, 'bob', 2) . "<br />\n";
echo substr_replace($var, 'bob', 0) . "<br />\n";
echo substr_replace($var, 'bob', 0, strlen($var)) . "<br />\n";

/* Insert 'bob' right at the beginning of $var. */
echo substr_replace($var, 'bob', 0, 0) . "<br />\n";

/* These next two replace 'MNRPQR' in $var with 'bob'. */
echo substr_replace($var, 'bob', 10, -1) . "<br />\n";
echo substr_replace($var, 'bob', -7, -1) . "<br />\n";

/* Delete 'MNRPQR' from $var. */
echo substr_replace($var, '', 10, -1) . "<br />\n";
?>

4. substr_compare

从主字符串中取出一定长度字符串str1二进制安全地比较str串
/*第一个参数是主串,第二参数是子串,第三个参数替换开始位置 第四个参数为结束位置 第五个参数表示是否区分大小写
为true表示区分 不写默认为不区分
int substr_compare ( string main_str, string str, int offset [, int length [, bool case_sensitivity]])
比如截得串为str1 与str比较
0 – 如果字符串相等
<0 – 如果str1 短于str
>0 – 如果str1 大于str

<?php
echo substr_compare("abcde", "bc", 1, 2); // 0
echo substr_compare("abcde", "bcg", 1, 2); // 0
echo substr_compare("abcde", "BC", 1, 2, true); // 0
echo substr_compare("abcde", "bc", 1, 3); // 1
echo substr_compare("abcde", "cd", 1, 2); // -1
echo substr_compare("abcde", "abc", 5, 1); // warning

?>

5. substr_count -- 子串出现的次数

int substr_count ( string haystack, string needle)
<?php
echo substr_count("This is a test", "is"); // prints out 2
?>

6. strstr

截取字符串,采用子串—结尾方式。
string strstr ( string haystack, string needle)
<?php
$email = 'user@example.com';
$domain = strstr($email, '@');
echo $domain; // prints @example.com
?>

7. strpos

查找子串在原串的出现位置
int strpos ( string haystack, string needle [, int offset])

如果 无offset 参数 从0 开始 有则从 offset 开始

不存在则返回false 存在返回其所在位置

由于php是弱类型的语言 false 和 0 的值是 等效的

所以判断时候要用 === 表示值等于同时值的类型也相等

<?php
$newstring = 'abcdef abcdef';
$pos_0 = strpos($newstring, 'a'); // $pos = 0
$pos_false = strpos($newstring, 'x'); // $pos = false
$pos = strpos($newstring, 'a', 1); // $pos = 7
?>

8. explode

分割字符串,将字符串毁成数组。
array explode ( string separator, string string [, int limit])
注意:
1).如果 separator字符串为空 则返回 false
2).如果 separator字符串中不含有 string 则返回 包含separator的数组
<?php
$post_str = " 100元";
$post_array = explode($post_str," ");
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
?>

9. implode

string implode ( string glue, array pieces)
连接字符串,将数组毁成一排。
注意:用时确保pieces为数组
<?php
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
?>

10. trim

string trim ( string str [, string charlist])
默认去掉左右空格

<?php

$text = "\t\tThese are a few words :) ... ";

echo trim($text); // "These are a few words :) ..."
echo trim($text, " \t."); // "These are a few words :)"

// trim the ASCII control characters at the beginning and end of $binary
// (from 0 to 31 inclusive)
$clean = trim($binary, "\x00..\x1F");

?>

11. str_pad

string str_pad ( string input, int pad_length [, string pad_string [, int pad_type]])

填充补齐
注意:如果 pad_string 不提供 则补齐的为空格 反之为 pad_string的值
pad_type 为补齐的方式不提供在右侧补齐
pad_type 值:左STR_PAD_LEFT 右STR_PAD_RIGHT 两端STR_PAD_BOTH
如果:input长度<pad_length 则不会进行补齐操作 补齐位数为(input长度-pad_length)
<?php
$input = "Alien";
echo str_pad($input, 10); // produces "Alien "
echo str_pad($input, 10, "-=", STR_PAD_LEFT); // produces "-=-=-Alien"
echo str_pad($input, 10, "_", STR_PAD_BOTH); // produces "__Alien___"
echo str_pad($input, 6 , "___"); // produces "Alien_"
?>

12. strtok

string strtok ( string arg1, string arg2)
arg1 为主串
arg2 为标记的字符串 可以是多个字节 如" \n\t" 空格\n\t或"im"
记号化字符串 按记号从string中得到子串

<?php
$string = "This is\tan example\nstring";
echo $string;
echo "<br/>";
/* Use tab and newline as tokenizing characters as well */
//eg1:
$tok = strtok($string, " \n\t");
echo $tok;
echo "<br/>";
while ($tok) {
echo "Word=$tok<br />";
$tok = strtok(" \n\t");
}
//eg2:
$tok = strtok($string, "im");
echo $tok;
echo "<br/>";
while ($tok) {
echo "Word=$tok<br />";
$tok = strtok("im");
}
?>
eg2:输出结果
Th
Word=Th
Word=s
Word=s an exa
Word=ple str
Word=ng

eg2:输出结果
This
Word=This
Word=is
Word=an
Word=example
Word=string
注意使用方法 搭配while使用
$tok = strtok($string, " \n\t");
while ($tok) {
echo "Word=$tok<br />";
$tok = strtok(" \n\t");//此处写法
}

13. strtoupper

string strtoupper ( string string)

转为大写
<?php
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtoupper($str);
echo $str; // Prints MARY HAD A LITTLE LAMB AND SHE LOVED IT SO
?>

14. strtolower

string strtolower ( string string)

转为小写
<?php
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtolower($str);
echo $str; // Prints mary had a little lamb and she loved it so
?>

15. strtotime

int strtotime ( string time [, int now])

将任何英文文本的日期时间描述解析为 UNIX 时间戳

<?php
echo strtotime ("now"), "\n";
echo strtotime ("10 September 2000"), "\n";
echo strtotime ("+1 day"), "\n";
echo strtotime ("+1 week"), "\n";
echo strtotime ("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime ("next Thursday"), "\n";
echo strtotime ("last Monday"), "\n";
?>

15. str_replace

mixed str_replace ( mixed search, mixed replace, mixed subject [, int &count])

普通式替换
第一个参数为查询内容
第二个参数为替换内容
第三个参数为要查的内容对象
第四个参数为返回查找出现的次数, 如提供则定义一个变量
<?php
// Provides: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");

// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");

// Provides: You should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);

// Use of the count parameter is available as of PHP 5.0.0
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count; // 2
?>

15. str_repeat

string str_repeat ( string input, int multiplier)
字符串重复
注意:多以for搭配输入一些信息
<?php
echo str_repeat("-=", 10);
?>
print : -=-=-=-=-=-=-=-=-=-=

16. str_rot13

string str_rot13 ( string str)
执行rot13的变化字符串

提示:编码和解码都是由相同的函数完成的。如果您把一个已编码的字符串作为参数,那么将返回原始字符串。
ROT-13 编码是一种每一个字母被另一个字母代替的方法。这个代替字母是由原来的字母向前移动 13 个字母而得到的。数字和非字母字符保持不变。

<?php
$new_str = str_rot13('PHP 4.3.0'); // CUC 4.3.0
echo $new_str;
echo "<br/>";
$old_str = str_rot13($new_str);
echo $new_str;
?>

17. str_shuffle
str_shuffle()
函数随机地打乱字符串中的所有字符

<?php
echo str_shuffle("Hello World");
?>
print :H elooWlrdl

18. str_split

str_split(string,length)
函数把字符串分割到数组中。
string 必需。规定要分割的字符串。
length 可选。规定每个数组元素的长度。默认是 1。

说明
如果 length 小于 1,str_split() 函数将返回 false。

如果 length 大于字符串的长度,整个字符串将作为数组的唯一元素返回

<?php
print_r(str_split("Hello",3));
?>
输出:
Array
(
[0] => Hel
[1] => lo
)

19. str_word_count

函数计算字符串中的单词数。

str_word_count(string,return,char)

string 必需。规定要检查的字符串。
return 可选。规定 str_word_count() 函数的返回值。
可能的值:
0 - 默认。返回找到的单词的数目。
1 - 返回包含字符串中的单词的数组。
2 - 返回一个数组,其中的键是单词在字符串中的位置,值是实际的单词。
return 可选。规定被认定为单词的特殊字符。该参数是 PHP 5.1 中新加的。

例子 1
<?php
echo str_word_count("Hello world!");
?>
输出:2

例子 2
<?php
print_r(str_word_count("Hello world!",1));
?>
输出:
Array
(
[0] => Hello
[1] => world
)
例子 3
<?php
print_r(str_word_count("Hello world!",2));
?>
输出:
Array
(
[0] => Hello
[6] => world
)
例子 4
<?php
print_r(str_word_count("Hello world & good morning!",1));
print_r(str_word_count("Hello world & good morning!",1,"&"));
?>
输出:
Array
(
[0] => Hello
[1] => world
[2] => good
[3] => morning
)
Array
(
[0] => Hello
[1] => world
[2] => &
[3] => good
[4] => morning
)

20. strtr

函数转换字符串中特定的字符。

语法
strtr(string,from,to)或者
strtr(string,array)参数
描述
string1 必需。规定要转换的字符串。
from 必需(除非使用数组)。规定要改变的字符。
to 必需(除非使用数组)。规定要替换的字符。
array 必需(除非使用 from 和 to)。一个数组,其中的键是原始字符,值是目标字符。

说明
如果 from 和 to 的长度不同,则格式化为最短的长度。

例子 1
<?php
echo strtr("Hilla Warld","ia","eo");
?>
输出:
Hello World

例子 2
<?php
$arr = array("Hello" => "Hi", "kphp" => "earth");
echo strtr("Hello kphp",$arr);
?>
输出:
Hi earth

21. strrev
字符反转
strrev(string)
描述
string 必需。规定要反转的字符串。
例子
<?php
echo strrev("Hello World!");
?>
输出:
!dlroW olleH
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: