您的位置:首页 > 编程语言 > PHP开发

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个符合条件后,找到的符合条件的字符的位置

$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
str1
which 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
str1
which does not contain any of the characters in
str2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: