Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: find a?

  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question find a?

    Hey, What function / how would I find if a string has something? Say I have a string like:
    PHP Code:
    $string 'Hello their! This is my post :) btw, my name is Justin.'
    Say I wanted to find out if my name "Justin" is inside that string..
    so:
    PHP Code:
    whateverfunction('Justin'$string
    So I think you should get my idea now though :P

    Thanks a lot!

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

  • #3
    New Coder
    Join Date
    Aug 2010
    Posts
    45
    Thanks
    0
    Thanked 14 Times in 14 Posts
    One way of doing it is the following:

    PHP Code:
    if (strpos($string'Justin') !== false)
      :
        return 
    true;
    endif; 
    This is case sensitive (use stripos for case insensitive), but will also return true if the string contains "ffafafdjustinDSd". preg_replace can be used if you want to prevent this latter example from returning true (as mentioned in the previous post).

  • #4
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    strpos() is the superior option in this case.

    There is also stripos() that will perform a case-insensitive match.


  •  

    Posting Permissions

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