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 4 of 4
  1. #1
    New Coder BatCountry's Avatar
    Join Date
    Jul 2007
    Location
    McMaynerberry, Texas
    Posts
    57
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question StrPos - Finding Substring within a String Question

    Hi again,
    I just wanted to know how to find every occurrence of a sub-string in a string?

    I would like to find the positions of every occurrence of a word within a sentence.

    Code:
    CodingForums is a web code and development forum
    In the above sentence, I would like to have an array with the positions of all occurences of the letters "or" (2 occurrences in this case). I'm sure strpos can do this, can any one provide a function or the code necessary to do this?

    Thanks

  • #2
    New Coder BatCountry's Avatar
    Join Date
    Jul 2007
    Location
    McMaynerberry, Texas
    Posts
    57
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Exclamation

    maybe I need to explain a bit, I just want to search a string and get the position of each occurrence of a string. With strpos you only get the first occurrence - and I'd like to get it for all of them.

  • #3
    New to the CF scene
    Join Date
    Sep 2007
    Location
    Newcastle upon Tyne
    Posts
    6
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Code:
    function findAllOccurences($Haystack, $needle, $limit=0)
    {
      $Positions = array();
      $currentOffset = 0;
      $count=0;
      while(($pos = strpos($Haystack, $needle, $offset))!==false && ($count < $limit || $limit == 0))
      {
       $Positions[] = $pos;
       $offset = $pos + strlen($needle);
       $count++;
      }
      return $Positions;
    }
    I cant take credit for this it is from the user contributed notes in the PHP function manual entry for STRPOS

    http://uk.php.net/strpos

    This is a great place to start when your stuck with something.

  • #4
    Regular Coder
    Join Date
    Sep 2007
    Location
    Grahamstown, South Africa
    Posts
    237
    Thanks
    6
    Thanked 17 Times in 17 Posts

    Gawd man, wasted some time.

    I wrote a function just to test my rusty skills. (I work in a travel agency now). I really like the function posted before mine. But seeing as I wrote this I'll give it to you anyway.

    PHP Code:
    <?php
    $full_string 
    "Hello there bobby dog. You rolling with the big boys now."//string to be searched through.
    $counter 0$x 0;//misc counters.
    $search_val "he"//string to search for
    $search_length strlen($full_string);//get the length of the string
    $search_val_length strlen($search_val);//get the length of the string to search for.
    $test strpos($full_string,$search_val);
    $first_letter $search_val{0};
    if (
    $test){
        while (
    $counter $search_length){
            if (
    strtolower($full_string{$counter}) === $first_letter){
                if ((
    strtolower(substr($full_string,$counter,$search_val_length)) == $search_val)){
                    
    $occurences[$x] = $counter "-" . ($counter $search_val_length 1);
                    
    $counter $counter $search_val_length 1;
                    
    $x++;
                    }
            }
        
    $counter++;
        }
    print_r($occurences);
    } else{ echo(
    "No occurences"); }
    ?>
    its lame, but was interesting (&challenging to some extent)...
    Last edited by idalatob; 10-10-2007 at 02:44 PM. Reason: removed a little echo I added....


  •  

    Posting Permissions

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