php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #12676 strpos() ignores first character in haystack
Submitted: 2001-08-09 15:22 UTC Modified: 2001-08-09 15:29 UTC
From: aragon at phat dot za dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.0.6 OS: FreeBSD 4.3
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: aragon at phat dot za dot net
New email:
PHP Version: OS:

 

 [2001-08-09 15:22 UTC] aragon at phat dot za dot net
When performing a string search with strpos() on a string variable the function totally ignores the first character. For example:

$astring = "abc defg hij kl";

if (strpos($astring, "abc")) {
  echo("found\n");
} else {
  echo("not found\n");
}


When running the above script "not found" is echoed instead when, in fact, "found" should be echoed. Here's my ./configure from phpinfo():

./configure' '--with-config-file-path=/usr/local/etc/php.standalone' '--disable-pear' '--enable-versioning' '--with-system-regex' '--disable-debug' '--enable-track-vars' '--without-gd' '--without-mysql' '--with-zlib' '--with-mysql=/usr/local' '--enable-ftp' '--with-curl=/usr/local' '--enable-sockets' '--prefix=/usr/local' 'i386--freebsd4.3'

A possible workaround would be:


$astring = "abc defg hij kl";
$astring = " " . $astring;

if (strpos($astring, "abc")) {
  echo("found\n");
} else {
  echo("not found\n");
}

The above code echoes "found".


Regards,
Aragon Gouveia

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-08-09 15:29 UTC] rasmus@php.net
Please read the documentation: http://php.net/strpos
Nothing is ignored, you are checking the returned value incorrectly.  strpos()
returns false when it doesn't find something and the position starting at 0 when it does find a match.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue May 06 09:01:28 2025 UTC