php字符串操作相关(3)
2009-04-18 13:53
351 查看
字符(串)查找相关函数:
1.int strpos ( string $haystack , mixed $needle [, int $offset= 0 ] ) (不区分大小写)
int stripos ( string $haystack , string $needle [, int $offset= 0 ] )(区分大小写)
用法:搜索$needle关键字在字符串中的位置,如果有非false的int返回值,那么这个值就是这个字符的开始位置,也就是说字符串中包含这个关键字.
$haystack:被搜索的字符串,$needle需要搜索的关键字,$offset开始搜索得位置.
strpos函数一般返回一个false的布尔值,这个值有时也可能是" ",或者0,这时如果要确定类型,请用===进行类型得确定。
<?php
$out = strpos("abv", "v");
$out2 = strpos("abv", "fg");
//参数3的特殊用法,忽略第n个符合条件后,找到的符合条件的字符的位置
var_dump($out); //output: int 2
var_dump($out2); //output: boolean false
?>
2.string strstr ( string $haystack , mixed $needle [, bool $before_needle= false ] )(区分大小写)
string stristr ( string $haystack , mixed $needle [, bool $before_needle= false ] )(不区分大小写)
用法:返回第一个关键字$needle在变量中开始出现的地方到结尾部分的字符。
<?php
$email = 'name@example.com';
$domain = stristr($email, 'a');
$email2 = 'name@exAmple.com';
$domain2 = strstr($email, 'A');
echo $domain2; // prints Ample.com A从exAmple中开始出现
echo $domain; // prints ample.com a从name开始出现
?>
3.int strspn ( string str1, string str2 [, int start [, int length]] )
Returns the length of the initial segment of
这句不知道怎么翻译,所以附上原文,我的理解是返回str1中含有str2的字符的个数
<?php
$var = strspn("42 is the answer", "1234567890");
//$var 的值为2,前一段字符串含有4和2
<?php echo strspn("foo", "o", 1, 2);
// 输出2
?>
?>
int strcspn ( string str1, string str2 [, int start [, int length]] )(与上一个函数用法相反)
Returns the length of the initial segment of
1.int strpos ( string $haystack , mixed $needle [, int $offset= 0 ] ) (不区分大小写)
int stripos ( string $haystack , string $needle [, int $offset= 0 ] )(区分大小写)
用法:搜索$needle关键字在字符串中的位置,如果有非false的int返回值,那么这个值就是这个字符的开始位置,也就是说字符串中包含这个关键字.
$haystack:被搜索的字符串,$needle需要搜索的关键字,$offset开始搜索得位置.
strpos函数一般返回一个false的布尔值,这个值有时也可能是" ",或者0,这时如果要确定类型,请用===进行类型得确定。
<?php
$out = strpos("abv", "v");
$out2 = strpos("abv", "fg");
//参数3的特殊用法,忽略第n个符合条件后,找到的符合条件的字符的位置
$newstring = 'abcdef abcdef'; $pos = strpos($newstring, 'a', 1); // $pos = 7, not 0
var_dump($out); //output: int 2
var_dump($out2); //output: boolean false
?>
2.string strstr ( string $haystack , mixed $needle [, bool $before_needle= false ] )(区分大小写)
string stristr ( string $haystack , mixed $needle [, bool $before_needle= false ] )(不区分大小写)
用法:返回第一个关键字$needle在变量中开始出现的地方到结尾部分的字符。
<?php
$email = 'name@example.com';
$domain = stristr($email, 'a');
$email2 = 'name@exAmple.com';
$domain2 = strstr($email, 'A');
echo $domain2; // prints Ample.com A从exAmple中开始出现
echo $domain; // prints ample.com a从name开始出现
?>
3.int strspn ( string str1, string str2 [, int start [, int length]] )
Returns the length of the initial segment of
str1which consists entirely of characters in
str2.
这句不知道怎么翻译,所以附上原文,我的理解是返回str1中含有str2的字符的个数
<?php
$var = strspn("42 is the answer", "1234567890");
//$var 的值为2,前一段字符串含有4和2
<?php echo strspn("foo", "o", 1, 2);
// 输出2
?>
?>
int strcspn ( string str1, string str2 [, int start [, int length]] )(与上一个函数用法相反)
Returns the length of the initial segment of
str1which does not contain any of the characters in
str2
相关文章推荐
- php字符串操作相关的函数
- PHP-字符串相关操作
- PHP字符串操作相关
- php字符串处理函数相关操作
- php字符串操作相关(2)
- python字符串、字符串处理函数及字符串相关操作
- php大力力 [024节]PHP中的字符串连接操作(2015-08-27)
- PHP常用字符串的操作函数
- PHP字符串的操作
- php 数组的相关操作
- PHP 字符串操作入门教程
- php学习笔记(七)对象操作相关
- PHP字符串操作常用函数
- PHP文件读写操作相关函数总结
- php 关于解析json数据的相关操作
- 字符串相关操作函数汇总
- C语言字符串相关操作及源码
- PHP数组操作相关的函数
- 常见问题及解决办法 整理之3(字符串相关操作)
- PHP字符串截取操作大全