PHP & 技术 14 May 2007 02:19 pm

php使用strpos注意

  1. mystring = 'abc';
  2. $findme   = 'a';
  3.  
  4.  
  5. if (strpos($mystring, $findme)){
  6.     echo '找到';
  7. }else{
  8.     echo '找不到';
  9. }

由于’a'是在第一个字符所以返回是’0′,’0′等于’false’所以显示’找不到’;

应使用以下方式

  1. mystring = 'abc';
  2. $findme   = 'a';
  3. $pos = strpos($mystring, $findme);
  4.  
  5. // Note our use of ===.  Simply == would not work as expected
  6. // because the position of 'a' was the 0th (first) character.
  7. if ($pos === false) {
  8.     echo "The string '$findme' was not found in the string '$mystring'";
  9. } else {
  10.     echo "The string '$findme' was found in the string '$mystring'";
  11.     echo " and exists at position $pos";
  12. }

Trackback This Post | Subscribe to the comments through RSS Feed

Leave a Reply