一、strpos()函数
strpos() 函数返回字符串在另一个字符串中第一次出现的位置。如果没有找到该字符串,则返回 false。语法结构为strpos(string,find,start)
参数 | 描述 |
---|---|
string | 必需。规定被搜索的字符串。 |
find | 必需。规定要查找的字符。 |
start | 可选。规定开始搜索的位置。 |
注:该函数对大小写敏感。如需进行对大小写不敏感的搜索,请使用 stripos()函数。
示例1:
<?php
echo strpos("Hello world!","wo");
?>
输出:
6
示例2:
<?php
if (strpos($_POST['email'], '@') === false) {
print 'There was no @ in the e-mail address!';
}
?>
以上通过post方法简单的检测邮件地址中是否含有@符号,注意这里一等要使用三个等号来表示或都用!= =这样来取非。
二、strlen()与strstr()
strlen(string)用于返回字符串的长度,如:
<?php
echo strlen("a");
?>
注:上例中的工一点要加引号,不加的话会报错PHP Notice: Use of undefined constant a - assumed 'a'
strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。
<?php
$email = 'name@example.com';
$domain = strstr($email, '@');
echo $domain; // 打印 @example.com
$user = strstr($email, '@', true); // 从 PHP 5.3.0 起
echo $user; // 打印 name
?>
1、如果search参数是数字,则搜索匹配数字 ASCII 值的字符。
2、该函数对大小写敏感。如需进行大小写不敏感的搜索,请使用 stristr()。
上面两个函数配合使用的示例如下(查找一串字符中的元音节字符的数量):
<?php
$string = "This weekend, I'm going shopping for a pet chicken.";
$vowels = 0;
for ($i = 0, $j = strlen($string); $i < $j; $i++) {
if (strstr('aeiouAEIOU',$string[$i])) {
echo $vowels++ . "<br/>";
}
}
//echo $vowels;
?>
三、substr()
substr() 函数返回字符串的一部分。其语法格式为substr(string,start,length) ,具体用法为
参数 | 描述 |
---|---|
string | 必需。规定要返回其中一部分的字符串。 |
start | 必需。规定在字符串的何处开始。 正数:在字符串的指定位置开始 负数:在从字符串结尾的指定位置开始 0:在字符串中的第一个字符处开始 |
length | 可选。规定要返回的字符串长度。默认是直到字符串的结尾。 正数:从 start 参数所在的位置返回 负数:从字符串末端返回 |
注:如果 start 是负数且 length 小于等于 start,则 length 为 0。
示例:
print substr('watch out for that tree',6,5);
//out f
print substr('watch out for that tree',17);
//t tree
print substr('watch out for that tree',20,5);
//ree
print substr('watch out for that tree',-6);
//t tree
print substr('watch out for that tree',-17,5);
//out f
print substr('watch out for that tree',15,-2);
//hat tr
print substr('watch out for that tree',-4,-1);
//tre
四、substr_replace()与str_replace()
1、substr_replace()
substr_replace():把字符串的一部分替换为另一个字符串,其语法结构为substr_replace(string,replacement,start,length)
参数 | 描述 |
---|---|
string | 必需。规定要检查的字符串。 |
replacement | 必需。规定要插入的字符串。 |
start | 必需。规定在字符串的何处开始替换。正数 – 在第 start 个偏移量开始替换负数 – 在从字符串结尾的第 start 个偏移量开始替换0 – 在字符串中的第一个字符处开始替换 |
length | 可选。规定要替换多少个字符。正数 – 被替换的字符串长度负数 – 从字符串末端开始的被替换字符数0 – 插入而非替换 |
<?php
echo substr_replace('abcdef', '###', 1); //输出 a###
echo substr_replace('abcdef', '###', 1, 2); //输出 a###def
echo substr_replace('abcdef', '###', -3, 2); //输出 abc###f
echo substr_replace('abcdef', '###', 1, -2); //输出 a###ef
?>
2、str_replace()
str_replace() 函数使用一个字符串替换字符串中的另一些字符。语法结构为 str_replace(find,replace,string,count)
参数 | 描述 |
---|---|
find | 必需。规定要查找的值。 |
replace | 必需。规定替换 find 中的值的值。 |
string | 必需。规定被搜索的字符串。 |
count | 可选。一个变量,对替换数进行计数。 |
注:该函数对大小写敏感。请使用 str_ireplace() 执行对大小写不敏感的搜索。
例1:
<?php
echo str_replace("world","John","Hello world!");
?>
输出:
Hello John!
例2:
<?php
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "Replacements: $i";
?>
输出:
Array
(
[0] => blue
[1] => pink
[2] => green
[3] => yellow
)
Replacements: 1
例3(及于数组的替换):
<?php
$find = array("Hello","world");
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_replace($find,$replace,$arr));
?>
输出:
Array
(
[0] => B
[1] =>
[2] => !
)