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 9 of 9

Thread: inStr Function

  1. #1
    New Coder
    Join Date
    May 2006
    Location
    Pennsylvania, USA
    Posts
    31
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Arrow inStr Function

    well, i've found this useful for many things so i figured i'd post it

    PHP Code:
    function inStr ($needle$haystack)
    {
      
    $needlechars strlen($needle); //gets the number of characters in our needle
      
    $i 0;
      for(
    $i=0$i strlen($haystack); $i++) //creates a loop for the number of characters in our haystack
      
    {
        if(
    substr($haystack$i$needlechars) == $needle//checks to see if the needle is in this segment of the haystack
        
    {
          return 
    TRUE//if it is return true
        
    }
      }
      return 
    FALSE//if not, return false

    Format for using.
    PHP Code:
    if(inStr("CodingForums""The best site ever is CodingForums"))
    {
      echo 
    "CodingForums is in this string";


  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Aint is the same as strstr() or strpos()?
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #3
    New Coder
    Join Date
    May 2006
    Location
    Pennsylvania, USA
    Posts
    31
    Thanks
    0
    Thanked 4 Times in 4 Posts
    not quite.

    this checks to see if the needle is in the haystack, it dosent modify the needle or haystack in anyway

  • #4
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    It's pretty close. It does get around the 0 (match at first char)/false (no match) confusion that sometimes causes bugs with strpos, though. You could state this more succinctly as:
    PHP Code:
    function inStr($needle$haystack)
    {
        return 
    strpos($haystack$needle) !== false;

    PS: More comments are not always better comments. If anyone needs guidance as to what 'return true' does, noting that it returns true isn't going to clarify it any.
    Last edited by ralph l mayo; 05-11-2006 at 01:03 AM.

  • #5
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by xanderman
    not quite.

    this checks to see if the needle is in the haystack, it dosent modify the needle or haystack in anyway
    Neither of the functions modify the arguments given to them.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #6
    Regular Coder
    Join Date
    Sep 2005
    Posts
    394
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Seems rather silly to me. StrPos is very very very fast. Think if you have 100,000 characters. StrPos would take a matter of seconds, your function would take a lot longer.

    Code:
    if(strpos("The best site ever is CodingForums", "CodingForums")) 
    { 
      echo "CodingForums is in this string"; 
    }

  • #7
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by lavinpj1
    Seems rather silly to me. StrPos is very very very fast. Think if you have 100,000 characters. StrPos would take a matter of seconds, your function would take a lot longer.

    Code:
    if(strpos("The best site ever is CodingForums", "CodingForums")) 
    { 
      echo "CodingForums is in this string"; 
    }
    Your example there demonstrated the wrong way to use the function as ralph l mayo mentioned above... Remember, when using strpos, if a match is found at offset 0 (starting from the start), 0 will be returned which will evaluate to false. You should use:

    PHP Code:
    <?php

    if( false !== strpos"...""..." ) ){
       echo 
    "...";
    }

    // Please forgive the dots... im very tired

    ?>

  • #8
    New to the CF scene
    Join Date
    Nov 2013
    Location
    UK | CANADA | INDIA
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I Tried using the code mentioned above but couldnt get any response. so finally we wrote our own code which is the simplest and best working code is

    function inStr($needle, $haystack)
    {
    ECHO $doma = strstr($haystack, $needle);
    if(strlen(trim($doma)) > 0) return TRUE;
    else return FALSE;
    }

    $heystack="www.ritzcons.com";
    $needle="www";

    calling above function will return true

  • #9
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    That function isn't necessary, as marek_mar pointed out back in 2006. You can achieve the same results with:
    PHP Code:
    if( false !== strpos$haystack$needle ) )
    {
        
    // do something

    Or, if you need it inside of a function, you can use: inStr Function
    Either option is going to be much faster than looping over the string.


  •  

    Posting Permissions

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