PHP常用字符串操作函数实例总结(trim、nl2br、addcslashes、uudecode、md5等)
web PHP 2019-04-16 1967 0
关于本站

“最难不过坚持”

本人承接扒站仿站,php网站维护,病毒查杀,网站编辑,网站改版,html制作

有需要网站维护,改版,病毒查杀,网站编辑,网站备案,html制作等相关的工作可以联系我。
本人有多年相关工作经验,也可提供免费咨询,交个朋友。
有需要探讨问题的朋友,也可以加我微信,共同探讨!
微信:15011482830 QQ:408917339

6280512 2594 39
最新评论
https://jueru.net/
评 留言
:weixiao:
评 留言
:shuijiao: :weiqu: :zhenbang: :leng:
评 留言
:yiwen: :yiwen: :yiwen: :yiwen:
评 EasySass: could not generate CSS file. See Output panel for details.
这个业务逻辑多少都有点奇怪了,阅读浏览次数增值在新闻详情页的控制器方法里setInc,这怎么还写进模型事件里了。如果非要用onAfterRead也可以,把新闻文章的内容单独分出来一个news_content表,然后把它和news做关联,然后给news_content表的onAfterRead事件做增值处理,这样点进新闻页内查询到文章内容时才会触发它。
评 TP6模型事件-查询后onAfterRead不好用
文章标签更多
ThinkPHP (254)
Mysql (58)
DedeCms (33)
jQuery (67)
证件照 (1)
setInc (4)
setDec (4)
onclick (5)

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 )
?>


版权声明:本篇文章来源于网络。 来源链接

相关推荐
php实现ZIP压缩文件解压缩,中文乱码解决方法(重要)
PHP | 2019-04-30 5538
直接上代码,具体代码里面都有注释。直接中文压缩文件解压到中文文件夹。 <?php // 需开启配置 php_zip.dll // phpinfo(); header("Content-type:text/html;charset=utf-8"); /* * $filename 被解压文件名 * $path 解压...
openssl_private_decrypt解密失败
PHP | 2019-01-16 5514
复制别人的解密程序,原程序可以解密,复制过来就不可以 一步步尝试发现,秘钥换行符有区别, 原秘钥有换行符,复制过来不知道什么时候把换行符清空了,成了一行字符串了。
评论:0条
评论加载中...
发表评论