php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #44029 Suggestion for new "substring exists" function
Submitted: 2008-02-03 07:43 UTC Modified: 2008-02-03 16:15 UTC
From: anon at example dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.2.5 OS:
Private report: No CVE-ID: None
 [2008-02-03 07:43 UTC] anon at example dot com
Description:
------------
What we often need to test for is just whether a substring exists 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 careful to test only with two specific formulations, using the strict equivalency operators:

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

or

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


That "anti-FALSE" approach is generally awkward, and if you use == or != by mistake you'll have ambiguity with a substring position of number 0.

It would be handy and much less prone to syntax mistakes, to have a dedicated "string exists" function which returns only TRUE or FALSE.

This might be "strex" or "strpres" ("string present").

Plus an equivalent case sensitive version -- striex or stripres.




Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-03 07:46 UTC] whatever at yahoo dot com
I entered this suggestion previously as bug #43825 -- which got closed 
as Bogus with no note.

Please explain why it is "bogus" if closing again. If tony2001@php.net 
thinks there is no usability problem with detecting the simple existence 
of a substring in a string, he should examine the commentary in 
http://us3.php.net/manual/en/function.strpos.php. If there's some other 
function that already does what I'm suggesting please indicate what it 
is.
 [2008-02-03 16:15 UTC] rasmus@php.net
We already have strstr()
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 07:01:29 2024 UTC