www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] comparing strings

  1. #1
    Join Date
    Jan 2010
    Posts
    39

    resolved [RESOLVED] comparing strings

    Hello everyone. I'm having an issue comparing strings:

    I am trying to find a case insensitive PHP function that checks if a string is a part of another string. For instance if you compare "Hello my Name is Teo" with "my nAMe" it would return true or 1. Is there something like this? I have a huge list of string functions but I can't find something that works...

    Thanks in advanced.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,537
    stripos(), for one.
    PHP Code:
    if(stripos($haystack$needle) !== false)
    {
       
    // it's in there

    Note the use of the !== false comparison, as it will return integer 0 if found at the start of the string, which would be treated as a false with a non-type-checking == or != operator.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2010
    Posts
    39
    Never mind, found it

    For the people having the same problem, i used substr_count(), which counts how many times a string appears in another string.

    PHP Code:
    if (substr_count($str1$substr)>0) {
    whatever;


  4. #4
    Join Date
    Jan 2010
    Posts
    39
    Thanks a lot man, I had used stripos() in the beginning but I did not know that check with false. Thanks though, substr_count() works like a charm
    Last edited by erevos; 01-16-2010 at 02:01 PM. Reason: didn't read whole reply first time :P

  5. #5
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    Kindly listen to the advice posted by others before claiming resolution.

    From PHP.net:

    substr_count() returns the number of times the needle substring occurs in the haystack string. Please note that needle is case sensitive.
    Your original question was quite clear in stating you wanted a case insensitive comparison, you even put that in upper case to emphasize.

    stripos(), available in PHP5, is case insensitive, strpos is not. Also, just so you know, the function you used doesn't count overlapped substrings and stripos is also binary safe, which can be handy in some situations.

    Sorry, but had to post this, no disrespect intended.

    -jim

  6. #6
    Join Date
    Jan 2010
    Posts
    39
    Sure, but I went around the case insensitive thing another way, so no worries, thanks though

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles