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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
15 + 13 = ?
Subscribe to this entry?

 
 [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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 23:01:28 2024 UTC