简介:本次介绍的是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;
?>
//输出如下:
//注意:需要在浏览器中“查看网页源代码”
天涯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中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools