字符串
字符串
去除空白或其他字符函数(trim,ltrim,rtrim)
trim($str[,$charlist]);
" " :普通空格符 "\t" :制表符 "\n" :换行符 "\r" :回车符 "\0" :空字节符 "\x0B" :垂直制表符
echo trim(" hello,world ")
echo trim("hello,world","h")
echo trim("34ssddfcs", "3s");
echo trim("abcacdefcda", "abc");
ltrim,rtrim
|
大小写转换函数(strtoupper,strtolower)
字符串查找函数(substr_count,strpos,strstr)
substr_count(string $haystack,string $needle[,int $offset=0[,int $length]])
strpos(string $haystack,string $needle[,int $offset=0])
strstr(string $haystack,string $needle[,bool $before_needle]) echo strstr("adbbfa1dsxf", "b"); echo strstr("adbbfa1dsxf", "b",true);
|
字符串替换函数
str_replace(search,replace,subject[,count])
echo str_replace("你好", "真的非常感谢这个", "你好世界!");
$a = "12345,咱一起学习吧!12345,不要放弃呀!"; echo str_replace(array(1, 2, 3, 4), array("c", "d", "e", "f"), $a);
|
与html标签相关的函数
主要的转换规则如下:
& (和号) 成为 &
" (双引号) 成为 "
' (单引号) 成为 '
< (小于) 成为 <
> (大于) 成为 >
$str4 = <<<START <p style="color:red;font-size:30px">我们的努力是有意义的!</p> START;
echo $str4 . "<br>\n";
echo htmlspecialchars($str4) . "<br>\n";
<p style="color:red;font-size:30px">我们的努力是有意义的!</p><br> <p style="color:red;font-size:30px">我们的努力是有意义的!</p><br>
strip_tags(string,allow)
echo strip_tags("<d><a></a><input>112123</d><strong>你好</strong>", "<d><a>") . "<br>\n";
echo strip_tags("<d><a></a><input>112123</d><strong>你好</strong>", "<d><a><strong>") . "<br>\n";
|
字符串截取函数
参数说明(取自菜鸟教程)
| 参数 |
说明 |
| string |
必需。规定要返回其中一部分的字符串。 |
| start |
必需。规定在字符串的何处开始。 + 正数 - 在字符串的指定位置开始 + 负数 - 在从字符串结尾的指定位置开始 + 0 - 在字符串中的第一个字符处开始 |
| length |
可选。规定要返回的字符串长度。默认是直到字符串的结尾。 + 正数 - 从 start 参数所在的位置返回 + 负数 - 从字符串末端返回 |
substr(string,start,length);
|
字符串分隔函数
explode(separator,string[,limit]);
print_r(explode(',',$str,0)); print "<br>";
print_r(explode(',',$str,2)); print "<br>";
print_r(explode(',',$str,-2));
Array ( [0] => one,two,three,four ) <br>Array ( [0] => one [1] => two,three,four ) <br>Array ( [0] => one [1] => two )
str_split(string,length);
print_r(str_split("hello,world", 3));
Array ( [0] => hel [1] => lo, [2] => wor [3] => ld )
|
字符串函数 还有很多,具体还得看需求,届时自己学习即可。