用最简单的实现最强的......

导航

php字符串操作相关(3)

字符(串)查找相关函数:

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,这时如果要确定类型,请用===进行类型得确定。

1<?php
2$out = strpos("abv", "v");
3$out2 = strpos("abv", "fg");
     //参数3的特殊用法,忽略第n个符合条件后,找到的符合条件的字符的位置   
     $newstring 'abcdef abcdef';
     $pos strpos($newstring'a'1); // $pos = 7, not 0

4var_dump($out);             //output:  int 2
5var_dump($out2);             //output: boolean false
6?>


2.string strstr ( string $haystack , mixed $needle [, bool $before_needle= false ] )(区分大小写)
   string stristr ( string $haystack , mixed $needle [, bool $before_needle= false ] )(不区分大小写)
用法:返回第一个关键字$needle在变量中开始出现的地方到结尾部分的字符。

Code


3.int strspn ( string str1, string str2 [, int start [, int length]] )
Returns the length of the initial segment of str1 which consists entirely of characters in str2.
这句不知道怎么翻译,所以附上原文,我的理解是返回str1中含有str2的字符的个数

1<?php
2  $var = strspn("42 is the answer", "1234567890");
3    //$var 的值为2,前一段字符串含有4和2
4    <?php echo strspn("foo", "o", 1, 2);
5    // 输出2
6?> 
7?>

int strcspn ( string str1, string str2 [, int start [, int length]] )(与上一个函数用法相反)
Returns the length of the initial segment of str1 which does not contain any of the characters in str2

posted on 2009-04-18 13:53  最简单的就是最好的  阅读(555)  评论(0编辑  收藏  举报