字符串 函数 [总结]

<?php
header("Content-type: text/html; charset=utf-8");
//字符串 函数
//addcslashes — 以 C 语言风格使用反斜线转义字符串中的字符。  addcslashes ( string $str , string $charlist ) 返回字符串,该字符串在属于参数 charlist 列表中的字符前都加上了反斜线。
echo addcslashes('foo[ ]', 'A..z').'<br>';
// 输出:\f\o\o\[ \]   所有大小写字母均被转义,但 [\]^_` 以及分隔符、换行符、回车符等也一并被转义了。
echo addcslashes("zoo['.']", 'z.').'<br>';//可以通过第二个参数控制转义范围
// 输出:\zoo['\.']
//addslashes — 使用反斜线引用字符串。返回字符串,该字符串为了数据库查询语句等的需要在某些字符前加上了反斜线。这些字符是单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符)。
$str = "Is your name O'reilly ?";
// 输出: Is your name O\'reilly?
echo addslashes($str).'<br>';
//chop — rtrim 的别名。 删除字符串末端的空白字符(或者其他字符)
//chr — 返回指定的字符
$str1 = "The string ends in escape: ";
$str1 .= chr(35); /* 在 $str 后边增加换码符 */
echo $str1.'<br/>';//The string ends in escape: #
/* 通常这样更有用 */
$str2 = sprintf("The string ends in escape: %c", 35);
echo $str2.'<br/>';//The string ends in escape: #
//chunk_split — 将字符串分割成小块
$str = "The string ends in escape";
echo chunk_split($str, 4,'<br>') ."\n";//每4个字符后面用<br>隔开!!!!

//convert_uuencode — 使用 uuencode 编码一个字符串
//convert_uudecode — 解码一个 uuencode 编码的字符串
$some_string = "test\ntext text\r\n";
echo convert_uuencode($some_string).'<br>';
echo convert_uudecode(convert_uuencode($some_string));
//count_chars — 返回字符串所用字符的信息
$data = "Two Ts and one F.";
foreach (count_chars($data, 1) as $i => $val) {
    echo "There were $val instance(s) of \"" , chr($i) , "\" in the string.\n";
}
/*
 There were 4 instance(s) of " " in the string.
There were 1 instance(s) of "." in the string.
There were 1 instance(s) of "F" in the string.
There were 2 instance(s) of "T" in the string.
There were 1 instance(s) of "a" in the string.
There were 1 instance(s) of "d" in the string.
There were 1 instance(s) of "e" in the string.
There were 2 instance(s) of "n" in the string.
There were 2 instance(s) of "o" in the string.
There were 1 instance(s) of "s" in the string.
There were 1 instance(s) of "w" in the string.
 */
//explode — 使用一个字符串分割另一个字符串,返回一个数组
//fprintf — 将格式化后的字符串写入到流
//htmlentities — 将字符转换为 HTML 转义字符
echo '<br>';
$str=' is the <s>dfd</s>opposite <b>of</b>';
echo  htmlentities($str).'<br>';
echo html_entity_decode(htmlentities($str)).'<br>';
//html_entity_decode — 与htmlentities()函数时反操作
//htmlspecialchars_decode — 将特殊的 HTML 实体转换回普通字符
//htmlspecialchars — 将特殊字符转换为 HTML 实体
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;
//implode — 将一个一维数组的值转化为字符串!!!
//join — 别名 implode  !!!
//lcfirst — 使一个字符串的第一个字符小写  !!!
//strtolower() - 把字符串转换为小写
//strtoupper() - 把字符串转换为大写
//ucfirst() - 把字符串中的首字符转换为大写
//ucwords() - 把字符串中每个单词的首字符转换为大写
//levenshtein — 计算两个字符串之间的编辑距离
//ltrim — 删除字符串开头的空白字符(或其他字符)
//md5_file — 计算指定文件的 MD5 散列值
//md5 — 计算字符串的 MD5 散列值
//money_format — 将数字格式化成货币字符串
//nl2br — 在字符串所有新行之前插入 HTML 换行标记
echo '<br>';
$string = "This\r\nis\n\ra\nstring\r";
echo nl2br($string);
//number_format — 以千位分隔符方式格式化一个数字!!!
//ord — 返回字符的 ASCII 码值
//parse_str — 将字符串解析成多个变量!!!!!!!!!!
$str = "first=value&arr[]=foo+bar&arr[]=baz";
// 推荐用法
parse_str($str, $output);
print_r($output);
//print — 输出字符串
//printf — 输出格式化字符串
$number = 9;
$str = "北京";
printf("在%s有 %u 百万辆自行车。",$str,$number);
//quoted_printable_decode — 将 quoted-printable 字符串转换为 8-bit 字符串
//quoted_printable_encode — 将 8-bit 字符串转换成 quoted-printable 字符串
//quotemeta — 转义元字符集  返回 在下面这些特殊字符前加 反斜线(\) 转义后的字符串。 这些特殊字符包含:. \ + * ? [ ^ ] ( $ )
$str = "Hello world. (can you hear me?)";
echo quotemeta($str);
//rtrim — 删除字符串末端的空白字符(或者其他字符)
//setlocale — 设置地区信息
//sha1_file — 计算文件的 sha1 散列值
//sha1 — 计算字符串的 sha1 散列值
//similar_text — 计算两个字符串的相似度
//sprintf — Return a formatted string
//sscanf — 根据指定格式解析输入的字符
//str_getcsv — 解析 CSV 字符串为一个数组
//str_ireplace — str_replace 的忽略大小写版本
//str_pad — 使用另一个字符串填充字符串为指定长度!!!!!!!!!!!!!!!!
echo '<br>';
$input = "Alien";
echo str_pad($input, 10).'<br>';                      // 输出 "Alien     "
echo str_pad($input, 10, "-=", STR_PAD_LEFT).'<br>';  // 输出 "-=-=-Alien"
echo str_pad($input, 9, "_", STR_PAD_BOTH).'<br>';   // 输出 "__Alien__"
echo str_pad($input,  6, "___").'<br>';               // 输出 "Alien_"
echo str_pad($input,  3, "*").'<br>';                 // 输出 "Alien"
//str_repeat — 重复一个字符串多少次
echo str_repeat("A", 10).'<br>';
//str_replace — 子字符串替换
//str_rot13 — 对字符串执行 ROT13 转换
//str_shuffle — 随机打乱一个字符串
$str = 'abcdef';
echo str_shuffle($str);
//str_split — 将字符串转换为数组!!!!!!
$str = "Hello Friend";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
echo '<br>';
//str_word_count — 返回字符串中单词的使用情况
//strcasecmp — 二进制安全比较字符串(不区分大小写)
//strchr — 别名 strstr ( string $haystack , mixed $needle [, bool $before_needle = FALSE ] ) 返回 haystack 字符串从 needle 第一次出现的位置开始到 haystack 结尾的字符串。
$email  = 'name@example.com';
$domain = strstr($email, '@');
echo $domain.'<br>'; // 打印 @example.com
$user = strstr($email, '@', true); // 从 PHP 5.3.0 起
echo $user.'<br>'; // 打印 name
//strcmp — 二进制安全字符串比较
//strcoll — 基于区域设置的字符串比较
//strcspn — 获取不匹配遮罩的起始子字符串的长度
//strip_tags — 从字符串中去除 HTML 和 PHP 标记
//stripcslashes — 反引用一个使用 addcslashes 转义的字符串
//stripos — 查找字符串首次出现的位置(不区分大小写)
//stripslashes — 反引用一个引用字符串   [与addslashes()相对]
//stristr — strstr 函数的忽略大小写版本
//strlen — 获取字符串长度
echo strlen('度');//3
//strnatcasecmp — 使用“自然顺序”算法比较字符串(不区分大小写)
//strnatcmp — 使用自然排序算法比较字符串
//strpbrk — 在字符串中查找一组字符的任何一个字符
$text = 'This is a Simple text.';
// 输出 "is is a Simple text.",因为 'i' 先被匹配
echo strpbrk($text, 'mi');
// 输出 "Simple text.",因为字符区分大小写
echo strpbrk($text, 'S');
//strpos — 查找字符串首次出现的位置!!!
//strrchr — 查找指定字符在字符串中的最后一次出现
//strrev — 反转字符串!!!
//strripos — 计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)
//strrpos — 计算指定字符串在目标字符串中最后一次出现的位置
//strspn — 计算字符串中全部字符都存在于指定字符集合中的第一段子串的长度。
//strstr — 查找字符串的首次出现!!!
//strtok — 标记分割字符串
//strtolower — 将字符串转化为小写!!!
//strtoupper — 将字符串转化为大写!!!
//strtr — 转换指定字符
//substr_count — 计算字串出现的次数
//substr_replace — 替换字符串的子串!!!!!!!!!!!!!!!!!!!!!
//substr — 返回字符串的子串
//mb_substr — 获取部分字符串(处理汉字)
//trim — 去除字符串首尾处的空白字符(或者其他字符)
//ucfirst — 将字符串的首字母转换为大写
//ucwords — 将字符串中每个单词的首字母转换为大写
//vfprintf — 将格式化字符串写入流
//vprintf — 输出格式化字符串
//vsprintf — 返回格式化字符串
//wordwrap — 打断字符串为指定数量的字串





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值