/*常用的字符串输出函数
  *
  * 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("/\(?  (\d{3})?  \)?  (?(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中用到的一些字符串函数的更多相关文章

  1. Mysql 字符串函数 详解

    字符串函数是最常用的一种函数了,如果大家编写过程序的话,不妨回过头去看看自己使用过的函数,可能会惊讶地发现字符串处理的相关函数占已使用过的函数很大一部分.MySQL中字符串函数也是最丰富的一类函数,表 ...

  2. awk 字符串函数

    awk 提供了许多强大的字符串函数,见下表: awk 内置字符串函数 gsub(r,s) 在整个 $0 中用 s 替代 r gsub(r,s,t) 在整个 t 中用 s 替代 r index(s,t) ...

  3. js进阶js中支持正则的四个常用字符串函数(search march replace split)

    js进阶js中支持正则的四个常用字符串函数(search march replace split) 一.总结 代码中详细四个函数的用法 search march replace split 二.js进 ...

  4. NeuCF源码中用到的模块(函数)

    论文:<Neural Collaborative Filtering>源码中用到的模块(函数) from keras.layers import Embedding, Input, Den ...

  5. ThinkPHP 模板substr的截取字符串函数

    ThinkPHP 模板substr的截取字符串函数在Common/function.php加上以下代码 /** ** 截取中文字符串 **/ function msubstr($str, $start ...

  6. SQL字符串函数

    LEN() :计算字符串长度(字符的个数.)datalength();//计算字符串所占用的字节数,不属于字符串函数.测试varchar变量与nvarchar变量存储字符串a的区别.见备注1.LOWE ...

  7. Python3中的字符串函数学习总结

    这篇文章主要介绍了Python3中的字符串函数学习总结,本文讲解了格式化类方法.查找 & 替换类方法.拆分 & 组合类方法等内容,需要的朋友可以参考下. Sequence Types ...

  8. TSQL 字符串函数:截断和查找

    字符串截断函数是指:Stuff 和 SubString,字符串查找函数是:CharIndex 和 PatIndex 一,SubString 截取子串 最常用的字符串函数,用于截取特定长度的子串. SU ...

  9. c#编程基础之字符串函数

    c#常用的字符串函数 例一: 获取字符串的大小写函数 ToLower():得到字符串的小写形式 ToUpper():得到字符串的大写形式 注意: 字符串时不可变的,所以这些函数都不会直接改变字符串的内 ...

随机推荐

  1. spring mvc 非注解形式

    目录(?)[+] webxml配置文件 注如果使用注解可以加上-- servlet上下文配置文件 test-servletxml 实体类Empjava StartController控制器 控制器Em ...

  2. *max_element函数和*min_element函数

    C++中*max_element(v.begin,v.end)找最大元素*min_element(v.begin,v.end)找最小元素. 数组: #include<iostream> # ...

  3. T-SQL应用,视图、存储过程、触发器、游标、临时表等

    sqlserver常用操作: 视图.存储过程.触发器.函数 --*********************批处理********************* --[在一个批处理中存有一个语法错误,则所有 ...

  4. SQL2-子查询、join查询

    SQL常用高级查询包括:Join查询.子查询. 子查询: USE flowershopdb --子查询:在一个select语句使用另一个select 语句作为条件或数据来源. --查询块:一个sele ...

  5. IOS 特定于设备的开发:基于加速计的滚动视图

    倾斜滚轮使用设备的内置加速计来控制在UIScrollView的内容周围移动.当用户调增设备时,材料会相应的下落,他不会把视图定位在屏幕上,而是把内容视图滚动到一个新的偏移位置. 创建这个界面的挑战在于 ...

  6. 如何实现win7和VirtualBox中Ubuntu系统共享文件夹

    设备: 1.win7 旗舰版    2.VirtualBox虚拟机    3.Ubuntu12.04 以前在VM虚拟机中可以直接进行复制就可以将win7系统的文件复制到虚拟机中,然后现在安装了Virt ...

  7. 实验时css层叠样式表不更新的情况

    自定义了CSS的样式,希望在页面中起作用.因为MVC中Views/Shared/_Layout.cshtml是所有视图的公共文件,如下: <!DOCTYPE html> <html& ...

  8. MySQl5.6最新安装

    http://www.cnblogs.com/xiongpq/p/3384681.html http://dev.mysql.com/doc/refman/5.5/en/source-configur ...

  9. PCB打样镀层问题

    现在大部分pcb打样所用PCB板一般有锡板和金板之分(一般根据客户要求和产品特 性选择),那么它们有什么区别呢?现在我将他们的区别作比较如下: 1. 从成本方面来说,锡板价格低,金板价格高. 2. 从 ...

  10. C++中类成员函数作为回调函数

    注:与tr1::function对象结合使用,能获得更好的效果,详情见http://blog.csdn.net/this_capslock/article/details/38564719 回调函数是 ...