Home > php教程 > PHP开发 > Excerpted from PHP manual [8] – String string processing function

Excerpted from PHP manual [8] – String string processing function

黄舟
Release: 2016-12-22 10:18:35
Original
1086 people have browsed it

简介:本次介绍的是PHP手册中用来对字符串进行各种操作的系统函数,下面天涯把最常用的进行详细说明。

addslashes — 使用反斜线引用字符串
stripslashes — 去除 addslashes() 添加的反斜线




//天涯PHP博客 http://blog.phpha.com
$phpha = 'TianYa say: "Hello World!"';
$phpha2 = addslashes($phpha);
$phpha3 = stripslashes($phpha2);
echo $phpha2 . '
' . $phpha3;
?>
//输出如下:
TianYa say: \"Hello World!\"
TianYa say: "Hello World!"


crc32 — 计算一个字符串的 crc32 多项式
crypt — One-way string encryption (hashing)
echo — 输出一个字符串
explode — 使用一个字符串分割另一个字符串
implode — 用一个字符串连接数组的键值合并成新的字符串




//天涯PHP博客 http://blog.phpha.com
$phpha = 'Hello,World,Hi,Skyline';
$phpha_explode = explode(',', $phpha);
$phpha_implode = implode('+', $phpha_explode);
print_r($phpha_explode);
echo $phpha_implode;
?>
//输出如下:
Array
(
[0] => Hello
[1] => World
[2] => Hi
[3] => Skyline
)
Hello+World+Hi+Skyline


htmlspecialchars — 把特殊字符转换成HTML实体符号
htmlspecialchars_decode — 把HTML实体符号转换陈特殊字符




//天涯PHP博客 http://blog.phpha.com
$phpha = ';
$phpha2 = htmlspecialchars($phpha);
$phpha3 = htmlspecialchars_decode($phpha2);
echo $phpha2;
echo $phpha3;
?>
//输出如下:
//注意:需要在浏览器中“查看网页源代码”
<a href="http://www.php1.cn/">
天涯PHP博客


ltrim — 去除字符串左侧指定的字符(默认为空格)
rtrim — 去除字符串右侧指定的字符(默认为空格)
trim — 去除字符串两侧指定的字符(默认为空格)




//Remove spaces by default
//Tianya PHP blog http://blog.phpha.com
$phpha = ' HelloWorld ';
echo '|' . ltrim($phpha) . ' |
';
echo '|' . rtrim($phpha) . '|
';
echo '|' . trim($phpha) . '|';
?> ;
//The output is as follows:
|HelloWorld |
| HelloWorld|
|HelloWorld|
//Remove the specified characters
//Tianya PHP Blog http://blog.phpha.com
$phpha_com = ' #HelloWorld#';
echo '|' . ltrim($phpha_com, '#') . '|
';
echo '|' . rtrim($phpha_com, '#') . '|< ;br />';
echo '|' . trim($phpha_com, '#') . '|';
?>
//The output is as follows:
|HelloWorld#|
|#HelloWorld|
|HelloWorld |


sprintf — format string




sprintf(format,arg1,arg2,arg++)
format can be of the following types
%% - Returns the percent sign
%b - Binary number
%c - Character according to ASCII value
%d - Signed decimal number
%e - Continuous notation (such as 1.5e+3)
%u - Unsigned decimal number
%f - Floating point number (local settings aware)
%F - Floating point number (not local settings aware)
%o - Octal number
%s - String
%x - Hexadecimal number (lower case letters)
%X - Hexadecimal number (upper case letters)
$str = "Hello";
$number = 123;
$txt = sprintf("%s world. Day number %u",$str,$number);
// Hello world. Day number 123
echo $txt;
?>


str_repeat — Repeat a string a specified number of times




echo str_repeat('Hello', 10);
?>
//The output is as follows:
HelloHelloHelloHelloHelloHelloHelloHelloHello


str_replace — Replace the specified string




echo str_replace("world","John","Hello world!");
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "替换次数: $i";
?>
//输出如下:
Hello John!
Array
(
[0] => blue
[1] => pink
[2] => green
[3] => yellow
)
替换次数: 1


str_split — 把字符串分割到数组中




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


strip_tags — 函数剥去 HTML、XML 以及 PHP 的标签
第二个参数为允许的标签,不会被过滤




echo strip_tags("Hello world!", "");
?>
//输出如下:
Hello world!


strpos — Returns the position of the first occurrence of a string in another string, case-sensitive
stripos — Returns the position of the first occurrence of a string in another string, case-insensitive




echo strpos('Hello World', 'Wo'); // 6
echo strpos('Hello World', 'wO'); // Empty
echo stripos('Hello World', ' wO'); // 6
?>


strstr — Search for the first occurrence of one string within another string
stristr — Same as above, case-insensitive
This function returns the rest of the string part (from matching point). Returns false if the searched string is not found




echo strstr('Hello World', 'Wo'); // World
echo strstr('Hello World', 'wO' ); // Empty
echo stristr('Hello World', 'wO'); // World
?>


strlen — Returns the length of the string




echo strlen('http://blog.phpha.com'); // 21
echo strlen('http://www.phpha.com'); // 20
?>


strtolower — 将字符串中大写字母转换为小写
strtoupper — 将字符串中小写字母转换为大写




echo strtoupper('Hello, World!'); // HELLO, WORLD!
echo strtolower('Hello, World!'); // hello, world!
?>


ucfirst — 将整个字符串第一个字母转换为大写
ucwords — 将字符串中每一个单词的首字母转换为大写




echo ucfirst('hello, world!'); // Hello, world!
echo ucwords('hello, world!'); // Hello, World!
?>


md5 — PHP中的MD5加密




// f9ee486a49ebce11b5118ba57ceb9419
echo md5('http://blog.phpha.com');
?>
 以上就是摘自PHP手册[8] – String字符串处理函数的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template