|  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
 [2011-01-19 13:55 UTC] rzufall36 at yahoo dot com
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
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)


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-19 16:26 UTC]
-Status: Open +Status: Bogus
 [2011-01-19 16:26 UTC]
Use strstr() or stristr()
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Oct 28 18:01:24 2020 UTC