php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #17277 strstr matches any variable surrounded by underscores
Submitted: 2002-05-16 17:10 UTC Modified: 2002-05-16 17:31 UTC
From: svemir at baltok dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 4.2.1 OS: Linux
Private report: No CVE-ID: None
 [2002-05-16 17:10 UTC] svemir at baltok dot com
This script:

<?php
 $needle = "foo";
 echo "1: ";
 echo strstr("Moo _bar_", "_$needle_");
 echo "<br>2: ";
 echo strstr("Moo _bar_", "_{$needle}_");
 echo "<br>3: ";
 echo strstr("Moo _foo_", "_{$needle}_");
 echo "<br>4: ";
 echo strstr("Moo !bar!", "!$needle!");
?>

Gives this output:

1: _bar_
2: 
3: _foo_
4:

Obviously, the line 1 is the problem. If both "haystack" and "needle" have the strings surrounded by underscores - the strings are matched even if they are actually different. This happens only if the "needle" has a variable between the underscores, and the variable is not surrounded by { and }.

Prehaps I am missing something and the combination of "_$" is supposed to mean something, but i did not see it in the documentation.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-05-16 17:15 UTC] robert dot cummings at webmotion dot com
_$ doesn't have anysignificance, but that trailing _ does,
since it will try to match $needle_ as the variable name
and that will be empty.
 [2002-05-16 17:31 UTC] derick@php.net
You're missing something indeed, _$needle_ is equivalent with "." . ${"needle_"} as underscores can be part of a variablename.
As $needle_ is empty, the needle evaluates to "_", which is in "Moo _bar_"

Not a bug > bogus.

Derick
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 13 16:01:29 2024 UTC