ID:          44334
 Comment by:  florian dot ember at gmail dot com
 Reported By: anon at example dot com
 Status:      Open
 Bug Type:    Feature/Change Request
 PHP Version: 5.2.5
 New Comment:

Did you know that you can define your own functions?

function substr_exists($haystack, $needle) {
        return strpos($haystack, $needle) !== false;
}

"Imagine someone new to coding not getting confused by that."
There is a big warning on the strpos() manual page.


Previous Comments:
------------------------------------------------------------------------

[2008-03-05 08:53:25] whatever at yahoo dot com

I have entered this suggestion twice already -- both times it's gotten

closed as bogus:

 - Ticket #43825 -- closed with no reason noted

 - Ticket #44029 -- closed with a note: "We already have strstr()".


But... strstr() is different than what I am proposing.

It can't return a simple TRUE if a substring is present.

And in fact the strstr() manual page says:

    "Note: If you only want to determine if a particular needle occurs

     within haystack, use the faster and less memory intensive 
function 
     strpos() instead."


So, I'm suggesting an improvement over strpos() -- and getting told to

use something worse that strpos(), as the reason my suggestion is 
"bogus".

Thus I am trying once more.

If anyone wants to close this as bogus, please explain why having a 
simple TRUE/FALSE test for the presence of a substring would not be a 
usability advantage over strpos(), and would not be a usability and 
performance advantage over strstr().

If anyone thinks strpos() is already ideal for this and has no issues,

please take a look at the commentary on the strpos() manual page, 
which discusses how very careful you have to be with the syntax to get

expected results. 

Imagine someone new to coding not getting confused by that.

Thank you.

------------------------------------------------------------------------

[2008-03-05 08:46:52] anon at example dot com

Description:
------------
What we often need to test for is simply whether a substring is present
in a string or not. For example, this is very common when examining a
user agent -- is "MSIE" there or not?

Currently you can use strpos(), but you have to be very careful to test
only with two specific formulations, using the strict equivalency
operators:

    if (strpos($x, $y) === FALSE)

  or

    if (strpos($x, $y) !== FALSE)


You have to use exactly "=== FALSE" and "!== FALSE" and no other syntax
to avoid ambiguity with a substring position of number "0". (See
discussions of this on strpos() man page.)

And having to say "not false" rather than "true" is just inelegant.

It would be handy and much less prone to syntax mistakes, to have a
dedicated "substring is present" function which returns only TRUE -- the
substring is there -- or FALSE -- the substring isn't there.

This might be "strpres()" ("string present"), with an equivalent
stripres().

Example:

  if ( strpres ($_SERVER['HTTP_USER_AGENT'], 'MSIE' ) == TRUE )


This is exactly the direct, elegant, non-ambiguous, non-confusing,
rational way to ask this question. strpos() is not. strstr() is not.



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=44334&edit=1

Reply via email to