php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53788 Add in_string() function similar to in_array()
Submitted: 2011-01-19 13:55 UTC Modified: 2011-01-19 16:26 UTC
From: rzufall36 at yahoo dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: Irrelevant OS: Ubuntu 10.04
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: rzufall36 at yahoo dot com
New email:
PHP Version: OS:

 

 [2011-01-19 13:55 UTC] rzufall36 at yahoo dot com
Description:
------------
It would be quite helpful to have a helper function in_string(), like in_array(), 
that checks if a substring is within some string. Sure, with strpos()/stripos(), 
this can be achieved, but not everyone understands that immediately (and the same 
could be said about in_array(), which can also be done quite simply with a 
foreach, but surely not everyone gets that). There's some code how that might 
work in PHP at MediaWiki 
http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/GlobalFunctions.p
hp?view=markup which is open-source and could be used.

Now it is the question if the third parameter should also be in PHP, but no 
matter which way, it would still probably not too difficult to create such a 
helper function.

Test script:
---------------
# MediaWiki's way of doing this

function in_string( $needle, $str, $insensitive = false ) {
    $func = 'strpos';
    if( $insensitive ) $func = 'stripos';
    return $func( $str, $needle ) !== false;
}

# don't use (bool)$func($str, $needle) because it can be 0 (first character)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-19 16:26 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2011-01-19 16:26 UTC] rasmus@php.net
Use strstr() or stristr()
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Dec 12 08:01:24 2019 UTC