space line
space line
space line
space line
space line
您现在的位置是: 技术文章>>

php中搜索查找字符串

发布日期 2008-10-8 6:52:24
作者 shemily
出处 重用网
浏览次数 ...
今日浏览 ...
本月浏览 ...
我要留言 我要留言
space line
摘要
在php中搜索字符,判断某个字符串中是否存在一定格式的字符
space line
详细内容

在编程中经常要用到查找字符串,或者判断字符串中是否存在某个字符,

其中的一个查找函数如下:

int strpos ( string $haystack, mixed $needle [, int $offset] )

第一个参数是被查找的字符串,

第二个参数是需要查找的字符串,

第三个可选参数是开始查找的位置;

函数返回值

如果能查找到,则返回字符第一次出现的位置;

如果不存在,则返回Booleanfalse

示例

<?php
$mystring 
´abc´
;
$findme   ´a´
;
$pos strpos($mystring$findme
);

// Note our use of ===.  Simply == would not work as expected
// 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
?>

注意:

判断是否存在,需要用恒等于号“===”来判断是否为false值。

 

space line
我要留言 我要留言    
space line
space line
space line
space line
space line
space line