欢迎访问淘淘IT!QQ:505342486

PHP常用字符串操作函数实例总结(trim、nl2br、addcslashes、uudecode、md5等)

时间:2019-04-16 10:09:05    来源:网络    浏览:254

PHP常用字符串操作函数


<?php 
// ucfirst
// 将字符串中的首字母转换为大写
$str = "string";
echo ucfirst($str); // String
echo "<hr><br>";

// ucwords()
// 将字符串中的每个单词的首字母大写
$ucword = "hello everyone!";
echo ucwords($ucword); // Hello Everyone!
echo "<hr><br>";

// lcfirst ( string $str )
// 将字符串的首字符小写
$str = "Hello World";
echo lcfirst($str)."<br>";
echo "<hr><br>";

// ltrim() rtrim() trim()
// 去除空格
$str = "123 This is a test.....";
echo ltrim($str, "0..9") . "<br>"; // 去除左侧的数字 //  This is a test.....
/* 
ltrim($str, "0..9")得到的字符串是" This is a test.....",左侧还有一个空格,可以用浏览器调试模式看,也可以打印字符串长度
echo strlen(ltrim($str, "0..9")) . "<br>"; // 20
echo strlen("This is a test.....") . "<br>"; // 19
*/
// chop()函数是rtrim()函数的别名
echo rtrim($str, ".") . "<br>"; // 去除右侧的. // 123 This is a test
echo trim($str, "0..9A..Z.") . "<br>"; //去除字符串两端的大写字母,数字还有. // This is a test
echo "<hr><br>";

// HTML相关的字符串格式化函数
// nl2br()
// 将字符串中的\n转换为"<br>"
$str = "this is \n hello world";
echo nl2br($str) . '<br>';
/* 
this is 
hello world 
*/
echo "<hr><br>";

// htmlspecialchars()
// 将html标记以字符的形式显示,不进行解释
$str = "<b>hello world</b>";
echo $str . "<br>"; // 粗体hello world
echo htmlspecialchars($str); // <b>hello world</b>
echo "<hr><br>";

// addcslashes
// 添加反斜线
echo addcslashes("foo[]", "A..z") . "<br>"; // \f\o\o\[\]
echo addcslashes("zoo['.']", 'A..z') . "<br>"; // \z\o\o\['.'\]
echo "<hr><br>";

// convert_uuencode()
// 利用uudecode的方法对字符串进行编码
$string = "hello world";
$str = convert_uuencode($string); // +:&5L;&\@=V]R;&0` ` 
echo $str . "<br>";
echo convert_uudecode($str) . "<br>"; // hello world
echo "<hr><br>";

// html_entity_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = 'UTF-8' ]] )
// 与htmlentities方法相反,将进行编码后的html字符转换为浏览器能够编译的形式
$a = "I want a bright <b>future</b>";
$b = htmlentities($a) . "<br>"; // I want a bright <b>future</b>
echo $b;
echo html_entity_decode($b); // I want a bright future加粗
echo "<hr><br>";

// htmlspecialchars_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 ] )
// 与htmlspecialchars函数相反,将HTML实体转换为字符
$c = htmlspecialchars($a); // I want a bright <b>future</b>
echo $c . "<br>";
echo htmlspecialchars_decode($c) . "<br>"; // I want a bright future加粗
echo "<hr><br>";

// md5_file ( string $filename [, bool $raw_output = false ] )
// 对文件进行md5加密
$string = "password";
$str = md5($string);
if ($str == "5f4dcc3b5aa765d61d8327deb882cf99") {
    echo "The password is right <br>";
} 
// 附一个md5解密的网址 https://www.somd5.com/
echo "<hr><br>";

// parse_str ( string $str [, array &$arr ] )
// 将一个字符串进行解析,解析成变量和数组的形式
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str, $input);
print_r($input); // Array ( [first] => value [arr] => Array ( [0] => foo bar [1] => baz ) )
echo "<hr><br>";

// string sha1_file ( string $filename [, bool $raw_output = false ] )
// 计算文件的散列值
foreach(glob("F:/wamp/www/php20190214/*.php") as $ent) {
    if (is_dir($ent)) {
        continue;
    } 
    echo $ent . "(SHA1:" . sha1_file($ent) . ")<br>";
} 
/* 
F:/wamp/www/php20190214/1.php(SHA1:bfb802a48d103120ce715f21cb0793e4fe27cad9)
F:/wamp/www/php20190214/2.php(SHA1:42671c987ce6c6c62dfe2170d74a1914441d3417)
F:/wamp/www/php20190214/conn.php(SHA1:ebe8f7f47ea6e339afd6b043a7540fc36f3e555a)
F:/wamp/www/php20190214/index.php(SHA1:8e208229251fb26407e604898b50cfca3a0d7823)
F:/wamp/www/php20190214/welcome.php(SHA1:7606f0c28390856ce703671dd288a19d0b2c0b58) 
*/
echo "<hr><br>";

// int similar_text ( string $first , string $second [, float &$percent ] )
// 计算两个字符串的相似度,通过引用方式传递第三个参数,similar_text() 将计算相似程度百分数。
$string1 = "rogerzhalili";
$string2 = "zhangjieroger";
if ($str = similar_text($string1, $string2, $percent)) {
    echo $str . "<br>"; // 5
	echo $string1 . " and " . $string2 . " has the similarity of:" . $percent . "<br>";
	// rogerzhalili and zhangjieroger has the similarity of:40
} 
echo "<hr><br>";

// string str_shuffle ( string $str )
// 打乱一个字符串
$string = "I want you to solve this problem";
echo str_shuffle($string) . "<br>";
echo "<hr><br>";

// array str_split ( string $string [, int $split_length = 1 ] )
// 按照指定的长度对字符串进行分割
$string = "this problem";
$arr = str_split($string, 3);
print_r($arr); // Array ( [0] => thi [1] => s p [2] => rob [3] => lem )
echo "<hr><br>"; 

// str_word_count ( string $string [, int $format = 0 [, string $charlist ]] )
// 统计字符串中单词的数量
$string = "this problem";
$count = str_word_count($string);
echo $count; // 2
echo "<hr><br>";

// int strripos ( string $haystack , string $needle [, int $offset = 0 ] )
// 以不区分大小写的方式查找指定字符串在目标字符串中最后一次出现的位置。与 strrpos() 不同,strripos() 不区分大小写。
// offset用于指定从那个位置开始查找
$haystack = 'ababcd';
$needle = 'Ab';
echo "the last " . $needle . " postion is:" . strripos($haystack, $needle) . "<br>"; // the last Ab postion is:2
echo strrpos($haystack, 'ab'); // 2
echo "<hr><br>";

// string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )
// 返回 haystack 字符串从 needle 第一次出现的位置开始到 haystack 结尾的字符串。 该函数区分大小写。如果想要不区分大小写,请使用stristr()。
$a = "the First test";
$needle = "Fi";
echo strstr($a, $needle) . "<br>"; // First test
if ($c = strstr($a, "Fio")) {
    echo "find" . $c . "<br>";
} else {
    echo "not find the string!<br>";
} 
// not find the string!
echo "<hr><br>";

// int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )
// 查找$needle子字符串在$haystack中出现的次数,$needle区分大小写
// 如果想不区分大小写,先用strtolower把字符串变成小写
$hay = "La la wa la wa wa lala";
echo substr_count($hay, "la") . "<br>"; // 4
echo "<hr><br>";

// int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )
// 正则匹配,将匹配后的结果存放到$matches(如果指定了$matches的话)
preg_match_all("/?(\d3)?? (?(1) [\-\s] ) \d{3}-\d{4}/x", "Call 555-1212 or 1-800-555-1212", $phones);
echo "<pre class='brush:php;toolbar:false'>";
print_r($phones); // 提示错误,正则表达式不太理解
echo "</pre>";
echo "<hr><br>";

// preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
// 搜索subject中匹配pattern的部分, 以replacement进行替换.
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${1}1,$3'; //第二个1没有意义,就是个字符,可以换成任何字符
echo preg_replace($pattern, $replacement, $string); // April1,2003
echo "<hr><br>";

// array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )
// 通过一个正则表达式分隔字符串.
$str = 'string';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars); // Array ( [0] => s [1] => t [2] => r [3] => i [4] => n [5] => g )
?>



来源网址:http://www.php.cn/php-weizijiaocheng-28087.html

有帮助
(0)
0%
没帮助
(0)
0%

网友评论

网友评论文明上网理性发言
昵称:

全部评论0条)

o(╯□╰)o 暂无评论,我要抢沙发。
栏目列表
点击排行
more