1. strlen()

计算字符串长度

2. substr

截取字符串,采用 起点—长度方式。
//特例需要记住 取字符串除了最后一位的所有字符
$rest = substr("abcdef", 0, -1);  // returns "abcde"

3. substr_replace 查找替换

string substr_replace ( string string, string replacement, int start [, int length])
第一个参数是原串,第二个参数是替换内容,第三个参数替换开始位置 第四个参数为结束位置
<?PHP
$var = 'ABCDEFGH:/MNRPQR/';
echo "Original: $var<hr />\n";
/*第一个参数是原串,第二个参数是替换内容,第三个参数替换开始位置 第四个参数为结束位置
//函数作用在$var中用 bob 替换 给定范围的内容,第三个参数为开始位置 第四个参数为结束位置
/* These two examples replace all of $var with 'bob'. */
echo substr_replace($var, 'bob', 2) . "<br />\n";
echo substr_replace($var, 'bob', 0) . "<br />\n";
echo substr_replace($var, 'bob', 0, strlen($var)) . "<br />\n";

/* Insert 'bob' right at the beginning of $var. */
echo substr_replace($var, 'bob', 0, 0) . "<br />\n";

/* These next two replace 'MNRPQR' in $var with 'bob'. */
echo substr_replace($var, 'bob', 10, -1) . "<br />\n";
echo substr_replace($var, 'bob', -7, -1) . "<br />\n";

/* Delete 'MNRPQR' from $var. */
echo substr_replace($var, '', 10, -1) . "<br />\n";
?>

4. substr_compare

从主字符串中取出一定长度字符串str1二进制安全地比较str串
/*第一个参数是主串,第二参数是子串,第三个参数替换开始位置 第四个参数为结束位置 第五个参数表示是否区分大小写
为true表示区分 不写默认为不区分
int substr_compare ( string main_str, string str, int offset [, int length [, bool case_sensitivity]])
比如截得串为str1 与str比较
0 – 如果字符串相等 
<0 – 如果str1 短于str
>0 – 如果str1 大于str

<?php
echo substr_compare("abcde", "bc", 1, 2); // 0
echo substr_compare("abcde", "bcg", 1, 2); // 0
echo substr_compare("abcde", "BC", 1, 2, true); // 0
echo substr_compare("abcde", "bc", 1, 3); // 1
echo substr_compare("abcde", "cd", 1, 2); // -1
echo substr_compare("abcde", "abc", 5, 1); // warning

?>

5. substr_count -- 子串出现的次数

int substr_count ( string haystack, string needle)
<?php
echo substr_count("This is a test", "is"); // prints out 2
?>

6. strstr

截取字符串,采用子串—结尾方式。
string strstr ( string haystack, string needle)
<?php
$email = 'user@example.com';
$domain = strstr($email, '@');
echo $domain; // prints @example.com
?>

7. strpos

查找子串在原串的出现位置  
int strpos ( string haystack, string needle [, int offset])

如果 无offset 参数 从0 开始 有则从 offset 开始

不存在则返回false 存在返回其所在位置

由于php是弱类型的语言 false 和 0 的值是 等效的

所以判断时候要用 === 表示值等于同时值的类型也相等

<?php
$newstring = 'abcdef abcdef';
$pos_0 = strpos($newstring, 'a'); //  $pos = 0
$pos_false = strpos($newstring, 'x'); //  $pos = false
$pos = strpos($newstring, 'a', 1); // $pos = 7
?>

8. explode

分割字符串,将字符串毁成数组。
array explode ( string separator, string string [, int limit])
注意:
1).如果 separator字符串为空 则返回 false 
2).如果 separator字符串中不含有 string 则返回  包含separator的数组
<?php
$post_str = " 100元";
$post_array = explode($post_str," "); 
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
?>

9. implode

string implode ( string glue, array pieces)
连接字符串,将数组毁成一排。
注意:用时确保pieces为数组
<?php
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
?>

10. trim

string trim ( string str [, string charlist])
默认去掉左右空格

<?php

$text = "\t\tThese are a few words :) ...  ";

echo trim($text);           // "These are a few words :) ..."
echo trim($text, " \t."); // "These are a few words :)"

// trim the ASCII control characters at the beginning and end of $binary
// (from 0 to 31 inclusive)
$clean = trim($binary, "\x00..\x1F");

?>

11. str_pad

string str_pad ( string input, int pad_length [, string pad_string [, int pad_type]])

填充补齐
注意:如果 pad_string 不提供 则补齐的为空格 反之为 pad_string的值
      pad_type 为补齐的方式不提供在右侧补齐
      pad_type 值:左STR_PAD_LEFT 右STR_PAD_RIGHT 两端STR_PAD_BOTH 
      如果:input长度<pad_length 则不会进行补齐操作 补齐位数为(input长度-pad_length)
<?php
$input = "Alien";
echo str_pad($input, 10);                      // produces "Alien     "
echo str_pad($input, 10, "-=", STR_PAD_LEFT);  // produces "-=-=-Alien"
echo str_pad($input, 10, "_", STR_PAD_BOTH);   // produces "__Alien___"
echo str_pad($input, 6 , "___");               // produces "Alien_"
?>

12. strtok

string strtok ( string arg1, string arg2)
arg1 为主串
arg2 为标记的字符串 可以是多个字节 如" \n\t" 空格\n\t或"im"
记号化字符串 按记号从string中得到子串

<?php
$string = "This is\tan example\nstring";
echo $string;
echo "<br/>";
/* Use tab and newline as tokenizing characters as well  */
//eg1:
$tok = strtok($string, " \n\t");
echo $tok;
echo "<br/>";
while ($tok) {
    echo "Word=$tok<br />";
    $tok = strtok(" \n\t");
}
//eg2:
$tok = strtok($string, "im");
echo $tok;
echo "<br/>";
while ($tok) {
    echo "Word=$tok<br />";
    $tok = strtok("im");
}
?>
eg2:输出结果
Th
Word=Th
Word=s 
Word=s an exa
Word=ple str
Word=ng

eg2:输出结果
This
Word=This
Word=is
Word=an
Word=example
Word=string
注意使用方法 搭配while使用
$tok = strtok($string, " \n\t");
while ($tok) {
    echo "Word=$tok<br />";
    $tok = strtok(" \n\t");//此处写法
}

13. strtoupper

string strtoupper ( string string)

转为大写
<?php
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtoupper($str);
echo $str; // Prints MARY HAD A LITTLE LAMB AND SHE LOVED IT SO
?>

14. strtolower

string strtolower ( string string)

转为小写
<?php
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtolower($str);
echo $str; // Prints mary had a little lamb and she loved it so
?>

15. strtotime

int strtotime ( string time [, int now])

将任何英文文本的日期时间描述解析为 UNIX 时间戳
 
<?php
echo strtotime ("now"), "\n";
echo strtotime ("10 September 2000"), "\n";
echo strtotime ("+1 day"), "\n";
echo strtotime ("+1 week"), "\n";
echo strtotime ("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime ("next Thursday"), "\n";
echo strtotime ("last Monday"), "\n";
?>

15. str_replace

mixed str_replace ( mixed search, mixed replace, mixed subject [, int &count])

普通式替换 
第一个参数为查询内容
第二个参数为替换内容
第三个参数为要查的内容对象
第四个参数为返回查找出现的次数, 如提供则定义一个变量
<?php
// Provides: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");

// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");

// Provides: You should eat pizza, beer, and ice cream every day
$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);

// Use of the count parameter is available as of PHP 5.0.0
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count; // 2
?>

15. str_repeat

string str_repeat ( string input, int multiplier)
字符串重复 
注意:多以for搭配输入一些信息
<?php
echo str_repeat("-=", 10);
?> 
print : -=-=-=-=-=-=-=-=-=-=

16. str_rot13

string str_rot13 ( string str)
执行rot13的变化字符串

提示:编码和解码都是由相同的函数完成的。如果您把一个已编码的字符串作为参数,那么将返回原始字符串。
ROT-13 编码是一种每一个字母被另一个字母代替的方法。这个代替字母是由原来的字母向前移动 13 个字母而得到的。数字和非字母字符保持不变。

<?php
$new_str =  str_rot13('PHP 4.3.0'); // CUC 4.3.0
echo $new_str;
echo "<br/>";
$old_str =  str_rot13($new_str);
echo $new_str;
?>

17. str_shuffle
str_shuffle() 
函数随机地打乱字符串中的所有字符

<?php
echo str_shuffle("Hello World");
?>
print :H elooWlrdl

18. str_split

str_split(string,length)
函数把字符串分割到数组中。
string 必需。规定要分割的字符串。 
length 可选。规定每个数组元素的长度。默认是 1。

说明
如果 length 小于 1,str_split() 函数将返回 false。

如果 length 大于字符串的长度,整个字符串将作为数组的唯一元素返回

<?php
print_r(str_split("Hello",3));
?>
输出:
Array
(
[0] => Hel
[1] => lo
)

19. str_word_count

函数计算字符串中的单词数。

str_word_count(string,return,char)

string 必需。规定要检查的字符串。 
return 可选。规定 str_word_count() 函数的返回值。
可能的值:
0 - 默认。返回找到的单词的数目。 
1 - 返回包含字符串中的单词的数组。 
2 - 返回一个数组,其中的键是单词在字符串中的位置,值是实际的单词。 
return 可选。规定被认定为单词的特殊字符。该参数是 PHP 5.1 中新加的。

例子 1
<?php
echo str_word_count("Hello world!");
?>
输出:2

例子 2
<?php
print_r(str_word_count("Hello world!",1));
?>
输出:
Array
(
[0] => Hello
[1] => world
)
例子 3
<?php
print_r(str_word_count("Hello world!",2));
?>
输出:
Array
(
[0] => Hello
[6] => world
)
例子 4
<?php
print_r(str_word_count("Hello world & good morning!",1));
print_r(str_word_count("Hello world & good morning!",1,"&"));
?>
输出:
Array
(
[0] => Hello
[1] => world
[2] => good
[3] => morning
)
Array
(
[0] => Hello
[1] => world
[2] => &
[3] => good
[4] => morning
)

20.  strtr

函数转换字符串中特定的字符。

语法
strtr(string,from,to)或者
strtr(string,array)参数 
描述 
string1 必需。规定要转换的字符串。 
from 必需(除非使用数组)。规定要改变的字符。 
to 必需(除非使用数组)。规定要替换的字符。 
array 必需(除非使用 from 和 to)。一个数组,其中的键是原始字符,值是目标字符。

说明
如果 from 和 to 的长度不同,则格式化为最短的长度。

例子 1
<?php
echo strtr("Hilla Warld","ia","eo");
?>
输出:
Hello World

例子 2
<?php
$arr = array("Hello" => "Hi", "kphp" => "earth");
echo strtr("Hello kphp",$arr);
?>
输出:
Hi earth

21.  strrev
字符反转
strrev(string)
描述 
string 必需。规定要反转的字符串。 
例子
<?php
echo strrev("Hello World!");
?>
输出:
!dlroW olleH

php开发中常用字符串函数总结相关推荐

  1. Python中常用字符串 函数-转

    转自http://blog.csdn.net/jiangnanandi/archive/2008/10/09/3041964.aspx 在python有各种各样的string操作函数.在历史上stri ...

  2. 【python基础】python中常用字符串函数详解

    文章目录 1 字符串查询(index,find) 2. 字符串大小写转换操作(upper.lower.swapcase.capitalize和title) 3. 字符串对齐(center,just和z ...

  3. oracle plsql 字符串长度,plsql中常用字符串函数

    1.ASCII 返回与指定的字符对应的十进制数; SQL> select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ') space f ...

  4. linux内核字符串逆序,Linux内核中常用字符串函数实现

    //列举了部分常用的strcpy,strcmp,strcat,strchr,strstr,strpbrk... char *strcpy(char *dest, const char *src) { ...

  5. php开发中常用函数总结,PHP开发中常用函数总结

    PHP开发中常用函数总结 发布于 2014-10-31 08:34:03 | 48 次阅读 | 评论: 0 | 来源: 网友投递 PHP开源脚本语言PHP(外文名: Hypertext Preproc ...

  6. Golang字符串中常用的函数

    Golang字符串中常用的函数 说明: 字符串在我们程序开发中,使用的是非常多的,常用的函数需要同学们掌握: 下面列出20种常用的字符串函数: 1)统计字符串的长度,按字节len(str) 2)字符串 ...

  7. 写出python字符串三种常用的函数或方法_python中几种常用字符串函数

    1.lower()把所有字符换成小写 2.upper()把所有字符换成大写 3.swapcase()大小写互换 4.title()把每个单词首字母大写,他是以所有英文字母的字符来区别是否为一个单词的, ...

  8. php 常用字符串函数

    以下列出开发中常用的字符串函数,以供自己需要的时候查阅 长度 strlen($string):得到字符串长度 字符串查找 strpos($string, $search[, $offset]):在指定 ...

  9. iOS开发中常用的方法

    iOS开发中常用的方法 系统弹窗: 过期方法: UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"确认报价" ...

最新文章

  1. Deepfake让罗伯特·德尼罗用流利的德语表演台词!差点忘了他是美国人
  2. C#使用DataContractJsonSerializer来进行JSON解析
  3. C语言中,scanf与scanf_s的简单区别
  4. [剑指offer]面试题28:字符串的排列
  5. 常用决策树集成模型Random Forest、Adaboost、GBDT详解
  6. plc模拟器软件_你的PLC和触摸屏为什么总是通讯不上?
  7. 深度学习的未来在单片机身上?
  8. 十大硬盘数据恢复软件
  9. 我的世界1.12.2java下载_我的世界1.12.2.2中文版下载 我的世界1.12.2.2中文版单机游戏下载...
  10. 全连MGRE与星型拓扑MGRE
  11. python利器app插件_python利器app
  12. 两个常用算法day1
  13. Codevs 侦探推理
  14. ildasm Reflector
  15. 《白帽子讲Web安全》memo0
  16. python 将16位 png 深度图转化为伪彩色图
  17. 迅雷同时下载的人数越多,BT下载越快的奥秘——另辟蹊径的P2P应用
  18. 『PyTorch』学习笔记 2 —— 模型 Finetune
  19. 微信小程序page页面下有多余空白区域(解决方法)
  20. ORA-01078和ORA-00109的解决方法

热门文章

  1. OpenCV 安卓编程示例:1~6 全
  2. 拉依达准则去除异常数据
  3. 我用Python制作了全国疫情地图,其实一点都不难!
  4. Android Studio显示“Hardcoded String XXX,should use @string resource”的解决方法2-1
  5. Meta-Learning之How to train your MAML
  6. 周志华机器学习笔记(一)
  7. uniapp全局数据(全局url、全局openId)
  8. java scriptrunner,java使用ScriptRunner执行sql文件
  9. O2O优惠券核销-数据分析
  10. 网易2018校招数据分析师笔试答案作答