php常用函数-字符串

WBOY
Freigeben: 2016-07-29 08:50:41
Original
1073 Leute haben es durchsucht

学习php的过程中,整理的一些常用的函数,这是字符串函数。

header("Content-Type:text/html;charset=UTF-8");
//删除两边(单边)空格或其它预定义字符
$str = "##hello world@@";
echo trim($str,'#,@')."
"; //hello world
echo ltrim($str,'#')."
"; //hello world@@
echo rtrim($str,'@')."
"; //##hello world
/* chop()是rtrim()的别名 */
//返回路径中的目录部分
echo dirname("c:/testweb/home.php")."
"; //c:/testweb
/** 字符串生成与转化 */
//把字符串填充为指定的字符串中
$str = "hello world";
echo str_pad($str,20,'.',STR_PAD_BOTH)."
"; //....hello world.....
echo str_pad($str,20,'.',STR_PAD_LEFT)."
"; //.........hello world
echo str_pad($str,20,'.',STR_PAD_RIGHT)."
"; //hello world.........
//重复使用指定的字符串
echo str_repeat(".",13)."
"; //.............
/* 13为重复的次数 */
//把字符串分割到数组中(包含空格,一个空格为1个字符)
$str = "my name is Junjun Liu";
$str1 = str_split($str,3);
print_r($str1); //Array ( [0] => my [1] => nam [2] => e i [3] => s J [4] => unj [5] => un [6] => Liu )
//反转字符串
$str = "imagecreatetruecolor";
echo strrev($str)."
"; //roloceurtetaercegami
//按照指定长度对字符串进行拆行处理
$str = "An example on a long word is: Supercalifragulisticlasdkjflasdjfalsdkakd";
echo wordwrap($str,20, "
");
//随机打乱字符串中所有的字符(数字被打乱时 间隔符号也会随着打乱)
$str = "a1,b2,c3";
echo str_shuffle($str)."
"; //1,ca32,b(随机的一种)
//将字符串解析成变量
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first."
"; // value
echo $arr[0]."
"; // foo bar
echo $arr[1]."
"; // baz
print_r($arr); //Array ( [0] => foo bar [1] => baz )
parse_str("id=23&name=John%20Adams",$myArray);
print_r($myArray); //Array ( [id] => 23 [name] => John Adams )
//通过千分位组来格式化数字
echo number_format("1000000"); //1,000,000
echo number_format("1000000",2); //1,000,000.00
echo number_format("1000000",2,",","."); //1.000.000,00
//大小写转换
echo strtolower('NAME'); //name
echo strtoupper('name'); //NAME
echo ucfirst('my name is Junjun Liu'); //My name is Junjun Liu
echo ucwords('my name is Junjun Liu'); //My Name Is Junjun Liu
//html关联标签
//把字符串转为HTML实体
$str = "John & 'Adams'";
echo htmlentities($str, ENT_COMPAT); //John & 'Adams'
//预定义字符转html编码
htmlspecialchars($str);
echo "
";
//把字符串中的 \n \r和回车换成
标签,实现换行输出
$string = "This\r\nis\n\ra\nstring\r";
echo nl2br($string);
/**
* This
* is
* a
* string
*/
//剥去HTML,XML,PHP的标签
$str="asdasd

woaini

";
echo strip_tags($str); //asdasdwoaini
echo "
";
//在指定的字符前添加反斜线转义字符串中字符
$str = "Hello, My name is John Adams.";
echo addcslashes($str,'m'); //Hello, \my na\me is John Ada\ms
//删除addcslashes反斜线
echo stripcslashes($str); //Hello, my name is John Adams.
echo "
";
//指定预定义字符前添加反斜线
$str = "Who's John Ad\ams?";
$str = addslashes($str);
echo $str; //Who\'s John Adams?
//删除反斜线
echo stripslashes($str)."
"; //Who's John Adams?
//在字符串中某些预定义的字符前添加反斜线(所有的字符被转义)
$str = "hello world.(can you hear me?)";
echo quotemeta($str)."
"; //hello world\.\(can you hear me\?\)
//ASCII返回字符
echo chr(34); //返回 "
//返回字符串中第一个字符的ASCII码值
echo ord(abc); //97
echo "
";
/** 字符串比较 */
/*
* 1:前者大
* -1:后者大
* 0:前后相等
*/
echo "不区分大小写比较两字符串:".strcasecmp("abc","abd")."
"; //-1
echo "区别大小写比较两字符串:" .strcmp("abd","Abd")."
"; //1
echo "区别大小写比较两字符串:" .strncmp("abcd","abcc",2)."
"; //0 /* 2是比较前n个字符串大小 */
echo "不区分大小写比较两字符串:".strncasecmp("abcd","abcc",4)."
"; //1
echo "区分大小(按自然顺序)写比较两字符串:".strnatcmp("abc2","abc12")."
"; //-1
echo "不区分大小(按自然顺序)写比较两字符串:".strnatcasecmp("Abc8","abc12")."
"; //-1
/* 字符串切割与拼接 */
//将字符串分成小块(空格也算)
$str="hello world hello world";
echo chunk_split($str,2,"#"); //he#ll#o #wo#rl#d #he#ll#o #wo#rl#d#
//切开字符串
$first_token = strtok('/something', '/');
$second_token = strtok('/');
var_dump($first_token); //string(9) "something"
var_dump($second_token); //bool(false)
//var_dump($first_token,$second_token);(同时打印两个变量)
$str = "This is an /example string";
$tok = strtok($str,"/"); //This is an
echo $tok;
$str = "Thisisan /example string";
$tok = strtok($str,"/"); //Thisisan
echo $tok;
//使用一个字符串为标志分割另一个字符串
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *
//将数组值用预订字符连接成字符串
$array = array('lastname', 'email', 'phone');
$a = implode(",", $array);
echo $a; // lastname,email,phone
//截取字符串
$str = "absadf";
echo substr($str,2,3); //sad
echo substr($str,-4,-1);//sad
/* 字符串查找和替换 */
//字符串替换操作,区分大小写 str_replace(被换字体,换成的字串,原字串)
$str = "1,2,3:4,5:6";
echo str_replace(",",":",$str)."
"; //1:2:3:4:5:6
echo str_replace(array(",",":"),";",$str)."
"; //1;2;3;4;5;6
echo str_replace(array(",",":"),array(";","#"),$str)."
"; //1;2;3#4;5#6
//字符串替换操作,不区分大小写
$str = "abcdefg";
echo str_ireplace("ABC","xyz",$str); //xyzdefg
//统计一个字符串在另一个字符串中出现的次数substr_count(在此字符串中搜索,被搜索的字符串,开始偏移的位置,指定偏移的最大位置)
$str1 = "name";
$str2 = "my name isname name";
echo substr_count($str2,$str1); //2
//替换字符串中某段为另一个字符串
$var = 'ABCDEFGH:/MNRPQR/';
echo "Original: $var
\n"; //Original: ABCDEFGH:/MNRPQR/
/* 这两个例子使用 "bob" 替换整个 $var。*/
echo substr_replace($var, 'bob', 0) . "
\n"; //bob
echo substr_replace($var, 'bob', 0, strlen($var)) . "
\n"; //bob
/* 将 "bob" 插入到 $var 的开头处。*/
echo substr_replace($var, 'bob', 0, 0) . "
\n"; //bobABCDEFGH:/MNRPQR/
/* 下面两个例子使用 "bob" 替换 $var 中的 "MNRPQR"。*/
echo substr_replace($var, 'bob', 10, -1) . "
\n"; //ABCDEFGH:/bob/
echo substr_replace($var, 'bob', -7, -1) . "
\n"; //ABCDEFGH:/bob/
/* 从 $var 中删除 "MNRPQR"。*/
echo substr_replace($var, '', 10, -1) . "
\n"; //ABCDEFGH://
//返回两字符串相似度
$str1 = "abcdefgadfsa";
$str2 = "acdrgwsaasdf";
echo ((similar_text($str1,$str2)/strlen($str1))*100)."%"."
"; //58.333333333333%
//字串查找
$str = "zhangsan";
echo strstr($str,"a")."
"; //angsan 从前面开始找a出现的位置并截取到最后(默认false)别名:strchr()
echo strstr($str,"a",true)."
"; //zh 从前面开始找a出现的位置并向前截取
echo strrchr($str,"a")."
"; //an 从后面开始找a并截取到最后
echo strpos($str,"a")."
"; //2 获取字符串首次出现a的位置
echo strpos($str,"a",3)."
"; //6 从3位置开始获取字符串出现a的位置
echo strrpos($str,"a")."
"; //6 获取字符串中a最后一次出现的位置
//转换指定字符
$trans = array("hello" => "hi", "hi" => "hello");
echo strtr("hi all, I said hello", $trans); //hello all, I said hi
echo strtr("baab", "ab", "01"); //1001
$trans = array("ab" => "01");
echo strtr("baab", $trans); //ba01
/*
* strstr():区分大小写
* stristr():不区分大小写
*/
/*
* strpos():区分大小写
* stripos()不区分大小写
* strrpos():区分大小写
* strripos():不区分大小写
*
*/
//返回计算字符串中全部字符都存在于指定字符集合中的第一段子串的长度。
$var = strspn("42 is the answer to the 128th question.", "1234567890");
echo $var; //2 因为 '42' 是 subject 中第一段全部字符都存在于 '1234567890' 的连续字符。
//获取不匹配遮罩的起始子字符串的长度
$a = strcspn('abcd', 'apple'); var_dump($a); //int(0)
$b = strcspn('abcd', 'banana'); var_dump($a); //int(0)
$c = strcspn('hello', 'l'); var_dump($c); //int(2)
$d = strcspn('hello', 'world'); var_dump($d); //int(2)
/* 字符串统计 */
//统计字符串含有的单词数(第三个参数????????????????????????????????????????????????????)
$str = "My name is John";
echo str_word_count($str); //4
print_r(str_word_count($str,1)); //Array ( [0] => My [1] => name [2] => is [3] => John )
print_r(str_word_count($str,2)); //Array ( [0] => My [3] => name [8] => is [11] => John )
//统计字符串长度
$str = ' ab cd ';
echo strlen($str); // 7
//统计字符串所有字母出现次数(0,255),每个字符出现的次数,用对应的ascii码值表示其出现的数量
$str = "aaaaasdfasdfwer;dlfgjjpoertuodbldbnlskjl;asfjoiwertowitwo";
echo "
";<br>//print_r(count_chars($str));<br>echo "
Nach dem Login kopieren
";
//md5
$str = "hello4521";
echo md5($str); //5af267d811a324fd640b7ad2199dfe14
echo "
";
/*
function getMd5($str){
return md5(md5($s)."tri");
}
*/
//md5_file()
$str = "ly.db";
echo md5_file($str); //2f2b2262ed0732d497c90bf62af96240

以上就介绍了php常用函数-字符串,包括了php,字符串方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage