网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > PHP > PHP入门安装 > 字符串函数库-搜索类型  

字符串函数库-搜索类型

2006-11-30  作者:bitsCN整理  来源:中国网管联盟  点评 投稿 收藏

兼容:(PHP 3, PHP 4, PHP 5)
strpos -- Find position of first occurrence of a string
查找字符在字符串第一次出现的位置

语法:int strpos ( string haystack, mixed needle [, int offset] )
返回值:整数
函数种类: 资料处理

内容说明:
英文:
Returns the numeric position of the first occurrence of needle in the haystack string. Unlike the strrpos(), this function can take a full string as the needle parameter and the entire string will be used.
If needle is not found, strpos() will return boolean FALSE.

If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
The optional offset parameter allows you to specify which character in haystack to start searching. The position returned is still relative to the beginning of haystack.

中文:
传回参数 needle在字串 haystack中第一次出现的位置,以数字表示。不像strrpos( ),此函式可以取参数 needle全部的字串,而且会使用全部的字串。如果找不到参数 needle,则传回 false。

网管bitscn_com



如果参数 needle不是字串时,它会转换成整数并且按照字元的顺序值来使用。
参数 offset允许你去指定在 haystack中从那一个字元开始搜寻,传回的位置依然是相对於 haystack的起点。

*值得注意的是 needle 只能是一个字符,中文字等就不适合了。

例子讲解:
<?php

$mystring
= 'abc';

$findme= 'a';

$pos = strpos($mystring, $findme);



// Note our use of ===.Simply == would not work as expected
网管联盟bitsCN@com


// because the position of 'a' was the 0th (first) character.

if ($pos === false) {

echo
\"The string '$findme' was not found in the string '$mystring'\\";

} else {

echo \\"The string '$findme' was found in the string '$mystring'\\";

echo \\" and exists at position $pos\\";

}



// We can search for the character, ignoring anything before the offset

$newstring = 'abcdef abcdef';

$pos = strpos($newstring, 'a', 1); // $pos = 7, not 0

?>

strpos()与substr_count()对比:
<?php

$mystring
= \"Hello Chris\\"; 网管bitscn_com

if (substr_count($mystring, \\"Hello\\") == 0)

echo \\"no\\";

// same as:

if (strpos($mystring, \\"Hello\\") === false)

echo \\"no\\";

?>


对比下面两个代码注意数字情况下,容易出现的错误,数字整数情况下不能看成字符串。
<?php

$val1
=123;

$val2=\"123,456,789\\";

if (strpos($val2, $val1)!==false) echo \\"matched\\";

else echo \\"not matched\\";

?>

结果为: not matched

<?php
网管网www.bitscn.com


$val1
=(string)123;

$val2=\"123,456,789\\";

if (strpos($val2, $val1)!==false) echo \\"matched\\";

else echo \\"not matched\\";

?>

结果为:not matched

  网管下载dl.bitscn.com


TAGs   类型   搜索   函数   字符串   the   string   needle   strpos   not      
 上一篇:Binary-safe function   下一篇:关于PHP字符串的基础知识总结
字符串函数库-搜索类型 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
最新推荐文章:
网管论坛交流: