PHP常用字符串操作函数实例总结(trim、nl2br、addcslashes、uudecode、md5等)
/*常用的字符串输出函数
*
* echo() 输出字符串
* print() 输出一个或多个字符串
* die() 输出一条信息,并退出当前脚本
* printf() 输出格式化字符串
* sprintf() 把格式化的字符串写入到一个变量中
*
*/ //ucfirst
//将字符串中的首字母转换为大写
$str="string";
echo ucfirst($str);
echo "<hr><br/>"; //ucwords()
//将字符串中的每个单词的首字母大写
$ucword="hello everyone!";
echo ucwords($ucword);
echo "<hr><br/>"; //ltrim() rtrim() trim()
//去除空格
$str="123 This is a test.....";
echo ltrim($str,"0..9")."<br/>"; //去除左侧的数字
echo rtrim($str,".")."<br/>";
echo trim($str,"0..9A..Z.")."<br/>"; //去除字符串两端的大写字母,数字还有.
//HTML相关的字符串格式化函数 //nl2br()
//将字符串中的\n转换为"<br/>"
$str="this is \n hello world";
echo nl2br($str).'<br/>'; //htmlspecialchars()
//将html标记以字符的形式显示,不进行解释
$str="<b>hello world</b>";
echo $str."<br/>";
echo htmlspecialchars($str);
echo "<hr><br/>"; //addcslashes
//添加反斜线
$str=addcslashes("foo[]","A..z");
echo $str."<br/>";
echo addcslashes("zoo['.']",'A..z')."<br/>"; //convert_uuencode()
//利用uudecode的方法对字符串进行编码
$string="hello world";
$str= convert_uuencode($string);
echo $str."<br/>";
echo convert_uudecode($str)."<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/>";
echo $b;
echo html_entity_decode($b);
echo "<hr><br/>"; //htmlspecialchars_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 ] )
//与htmlspecialchars函数相反,将HTML实体转换为字符
$c=htmlspecialchars($a);
echo $c."<br/>";
echo htmlspecialchars_decode($c)."<br/>";
echo "<hr><br/>"; //lcfirst ( string $str )
//将字符串的首字符小写
$str="Hello World";
// echo lcfirst($str)."<br/>"; //md5_file ( string $filename [, bool $raw_output = false ] )
//对文件进行md5加密
//
$string="password";
$str=md5($string);
if($str=="5f4dcc3b5aa765d61d8327deb882cf99"){
echo "The password is right <br/>";
} //parse_str ( string $str [, array &$arr ] )
//将一个字符串进行解析,解析成变量和数组的形式
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str,$input);
print_r($input);
echo "<hr><br/>"; //string sha1_file ( string $filename [, bool $raw_output = false ] )
//计算文件的散列值
foreach(glob("C:/lamp/appache2/htdocs/*.php") as $ent){
if(is_dir($ent)){
continue;
}
echo $ent."(SHA1:".sha1_file($ent).")<br/>";
}
echo "<hr><br/>"; //int similar_text ( string $first , string $second [, float &$percent ] )
//计算两个字符串的相似度,通过引用方式传递第三个参数,similar_text() 将
//计算相似程度百分数。
$string1="rogerzhalili";
$string2="zhangjieroger";
if(similar_text($string1,$string2,$percent)){
echo $string1." and ".$string2." has the similarity of:".$percent."<br/>";
}
echo "<hr><br/>"; //string str_shuffle ( string $str )
//打乱一个字符串
$string="I want you to solve this problem";
echo str_shuffle($string)."<br/>"; //array str_split ( string $string [, int $split_length = 1 ] )
//按照指定的长度对字符串进行分割
$arr=str_split($string,3); //str_word_count ( string $string [, int $format = 0 [, string $charlist ]] )
//统计字符串中单词的数量
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/>";
echo strrpos($haystack,'ab');
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/>";
if($c=strstr($a,"Fio")){
echo "find".$c."<br/>";
}
else
{
echo "not find the string!<br/>";
}
echo "<hr><br/>"; //int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )
//查找$needle子字符串在$haystack中出现的次数,$needle区分大小写
$hay="la la wa la wa wa lala";
echo substr_count($hay,"la")."<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>";
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';
echo preg_replace($pattern,$replacement,$string);
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);
PHP常用字符串操作函数实例总结(trim、nl2br、addcslashes、uudecode、md5等)的更多相关文章
- [转载]c++常用字符串操作函数
原文地址:c++常用字符串操作函数作者:Valsun 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source ...
- mysql常用字符串操作函数大全,以及实例
今天在论坛中看到一个关于mysql的问题,问题如下 good_id cat_id12654 665,56912655 601,4722 goods_id是商品i ...
- C语言的常用字符串操作函数(一)
一直做的是单片机相关的程序设计,所以程序设计上更偏向底层,对于字符串的操作也仅限于液晶屏幕上的显示等工作,想提高下字符串操作的水平,而不是笨拙的数组替换等方式,翻看帖子发现C语言的字符串操作函数竟然这 ...
- VB常用字符串操作函数
1. ASC(X),Chr(X):转换字符字符码 [格式]: P=Asc(X) 返回字符串X的第一个字符的字符码 P=Chr(X) 返回字符码等于X的字符 [范例]: (1)P=Chr(65) ‘ 输 ...
- string常用字符串操作函数
1.strdup和strndup 说明:strdup() 函数将参数 s 指向的字符串复制到一个字符串指针上去,这个字符串指针事先可以没被初始化.在复制时,strdup() 会给这个指针分配空间,使用 ...
- golang——(strings包)常用字符串操作函数
(1)func HasPrefix(s, prefix string) bool 判断字符串s是否有前缀字符串prefix: (2)func HasSuffix(s, suffix string) b ...
- LoadRunner中常用的字符串操作函数
LoadRunner中常用的字符串操作函数有: strcpy(destination_string, source_string); strc ...
- JS 常用字符串操作
Js字符串操作函数大全 /******************************************* 字符串函数扩充 ...
- C++常用字符串分割方法实例汇总
投稿:shichen2014 字体:[增加 减小] 类型:转载 时间:2014-10-08我要评论 这篇文章主要介绍了C++常用字符串分割方法实例汇总,包括了strtok函数.STL.Boost等常用 ...
随机推荐
- yourphp读取不到hits
源代码 <YP:list name="Article" order="id desc" catid="37" limit=" ...
- Form表单中的action路径问题,form表单action路径《jsp--->Servlet路劲问题》这个和上一个《jsp--->Servlet》文章有关
Form表单中的action路径问题,form表单action路径 热度5 评论 50 www.BkJia.Com 网友分享于: 2014-08-14 08:08:01 浏览数44525次 ...
- jQuery监听键盘事件及相关操作使用教程
一.首先需要知道的是: 1.keydown() keydown事件会在键盘按下时触发. 2.keyup() keyup事件会在按键释放时触发,也就是你按下键盘起来后的事件 3.keypress() k ...
- 工具,如何去掉百度编辑器 ueditor 元素路径、字数统计等
去掉如下截图: 在百度编辑器 ueditor 根目录下: ueditor.config.js 文件中 搜索并将参数elementPathEnabled设置成false即可 常用功能开关如下: ,ele ...
- HBase命令(一) -- 库操作
打开数据库 bin/start-hbase.sh //打开HBase bin/hbase shell //以命令行的方式打开Hbase控制台 Rest接口开启 bin/hbase rest //普通的 ...
- C语言中的参数传递
有空看看: c语言 函数传输传递的三种方式(值.指针.引用) C语言之参数传递 C语言形参和实参,传值调用和引用调用的区别
- HTML5实战与剖析之触摸事件(touchstart、touchmove和touchend)(转)
HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享.今天为大家介绍的事件主 ...
- PPTP(Point to Point Tunneling Protocol),即点对点隧道协议。
PPTP PPTP(Point to Point Tunneling Protocol),即点对点隧道协议.该协议是在PPP协议的基础上开发的一种新的增强型安全协议,支持多协议虚拟专用网(VPN),可 ...
- 性能:15个JavaScript本地存储技术的函数库和工具
当构建更复杂的JavaScript应用程序运行在用户的浏览器是非常有用的,它可以在浏览器中存储信息,这样的信息可以被共享在不同的页面,浏览会话. 在最近的过去,这将有可能只被cookies文本文件保存 ...
- MEAN组合框架搭建教程
1,我们先走在官方github里面下载个包文件: git clone https://github.com/linnovate/mean.git (是慢了点) 2,我把这个文件解压后文件名叫mean ...