Find last occurrence of a string in a string [RESOLVED]-VBForums
Results 1 to 8 of 8

Thread: Find last occurrence of a string in a string [RESOLVED]

  1. #1

    Thread Starter
    ASP.NET Moderator mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174

    Resolved Find last occurrence of a string in a string [RESOLVED]

    I do not have PHP 5, so I cannot use strrpos().

    Does anyone have a code snippet to find the last occurrence of a string in a string for pre-PHP5?

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Camden, London Mode: PPI
    Posts
    4,904

    Re: Find last occurrence of a string in a string

    You could use the strrpos() function.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    ASP.NET Moderator mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174

    Re: Find last occurrence of a string in a string

    strrpos -- Find position of last occurrence of a char in a string

    And,

    The needle may be a string of more than one character as of PHP 5.0.0.

    So I can't use that.

    What I ended up doing though, was:

    PHP Code:
    $largestring //whatever
    $reverselarge strrev($largestring);

    $posoflastchevron strlen($largestring) - strpos($reverselarge";781#;pma&"); 
    Although I don't quite like this method.

  4. #4

    Thread Starter
    ASP.NET Moderator mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174

    Re: Find last occurrence of a string in a string

    Hmm... just saw this:

    PHP Code:

       
    function strepos($haystack$needle$offset=0) {       
           
    $pos_rule = ($offset<0)?strlen($haystack)+($offset-1):$offset;
           
    $last_pos false$first_run true;
           do {
               
    $pos=strpos($haystack$needle, (intval($last_pos)+(($first_run)?0:strlen($needle))));
               if (
    $pos!==false && (($offset<&& $pos <= $pos_rule)||$offset >= 0)) {
                   
    $last_pos $pos;
               } else { break; }
               
    $first_run false;
           } while (
    $pos !== false);
           if (
    $offset>&& $last_pos<$pos_rule) { $last_pos false; }
           return 
    $last_pos;
       } 

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Camden, London Mode: PPI
    Posts
    4,904

    Re: Find last occurrence of a string in a string

    Sorry - I didn't read your question.

    Although your solution need only be a few lines. This works fine
    PHP Code:
        function strrpos_new($haystack$needle$offset 0)
        {
            
    /* set initial return value to false, just incase the string
               is never found */
            
    $old_pos false;
            
            
    /* use strpos() until no match is found */
            
    while (($pos strpos($haystack$needle$offset)) !== false) {
                
    $old_pos $pos;
                
    $offset $old_pos strlen($needle);
            }

            
    /* return the old position */
            
    return $old_pos;
        } 
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Camden, London Mode: PPI
    Posts
    4,904

    Re: Find last occurrence of a string in a string

    And this version can handle negative offsets
    PHP Code:
        function strrpos_new($haystack$needle$offset 0)
        {
            
    /* check for a negative offset and assertain where
               searching should stop */
            
    if ($offset 0) {
                
    $stop strlen($haystack) + $offset 1;
                
    $offset 0;
            } else {
                
    $stop strlen($haystack) -1;
            }

            
    /* set initial return value to false, just incase the string
               is never found */
            
    $old_pos false;
            
            
    /* use strpos() until no match is found */
            
    while (($pos strpos($haystack$needle$offset)) !== false
                    
    && $pos <= $stop) {
                
    $old_pos $pos;
                
    $offset $old_pos strlen($needle);
            }

            
    /* return the old position */
            
    return $old_pos;
        } 
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7

    Thread Starter
    ASP.NET Moderator mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174

    Re: Find last occurrence of a string in a string

    Be a doll and add [Resolved] to the thread title, will ya?

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Camden, London Mode: PPI
    Posts
    4,904

    Re: Find last occurrence of a string in a string

    I shall require a fee for completion of such a service.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.