php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #44334 Suggestion for a "Substring is Present" Function
Submitted: 2008-03-05 08:46 UTC Modified: 2018-03-31 11:11 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: anon at example dot com Assigned:
Status: Suspended Package: Strings related
PHP Version: 5.2.5 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: anon at example dot com
New email:
PHP Version: OS:

 

 [2008-03-05 08:46 UTC] 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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-05 08:53 UTC] 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-22 22:16 UTC] florian dot ember at gmail dot com
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.
 [2016-12-31 00:17 UTC] cmb@php.net
-Package: Feature/Change Request +Package: Strings related
 [2018-03-31 11:11 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2018-03-31 11:11 UTC] cmb@php.net
> Did you know that you can define your own functions?

That!  If anybody still feels that this function should be added
to PHP, please start the RFC process[1].  For the time being, I'm
suspending this ticket.

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu May 23 01:01:32 2019 UTC