SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2000
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How's it going?


    I'm trying to find the last occurrence of a string within another string, and thought I'd be able to use strrpos().

    I've now found out that unlike strpos() we're limited to only 1 character.

    e.g. if we're looking for last occurrence of '<LI>' in haystack, strrpos will return the last occurrence of '<'
    not '<LI>'. This is bcs <LI> has 4 characters, not 1.

    $haystack = 'start<LI>middle<LI>ending<>';
    $position_end = strrpos($haystack, "<LI>");

    Does anyone know the most efficient workaraound so that strrpos can effectively accept greater than 1 character please?

    Thank you.....

    Jason

  2. #2
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a work around. Nice and clean!

    $haystack = 'start<LI>middle<LI>ending<>';
    $match = "<LI>";
    $position_end = strrpos($haystack, $match) + strlen($match);
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2000
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Son Nguyen for trying, but unless I'm missing something, this doesn't do it. It returns total length of string + 1, with the example I gave.

    I'd imagine that the reason why strrpos() permits just 1 character is bcs it requires quite a bit of effort to locate the last occurrence with a simple function? (especially as haystack could be much bigger with many more occurrences of the string)

    1 way I've thought of is to 1st replace all the occurrences of the string with 1 character and then use strrpos() to find the position.

    But, the problem is generating a unique character that doesn't clash. Please assume, that the contents of $string may or may not be known before-hand.

    Therefore, another check would be needed to make sure this character is uniwue and doesn't already occur in then string. If it does then try another character, and so on.

    But, this does not seem efficient.

    Cheers....

    Jason




  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2000
    Location
    Sydney, Australia
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why dont you just write your own function similar to strpos, but instead of searching for a single character, it will give you the llast position of a group of chracters, using the builtin substr function.

    that will search for the last position of a $needle in a $haystack

    (i havent tested it out, but it should give you an idea)

    e.g
    Code:
    function myStrPos ($needle, $haystack) {
       $needleLength = strlen($needle);
       $currentPos = -1;
       for ($i = 0; $i < (strlen($haystack) - $needleLength); $i++) {
           if (substr($haystack, $i, $i + $needleLength)) == $needle) {
               if ($i > $currentPos)
                   $currentPos = $i;
           }
    
       }
    
       $return $currentPos;
    }
    so if the function does not find an occurence of $needle in the $haystack, it will return a -1.

    If anyone can find bugs in this function than please go ahead and correct it!! =)
    http://www.myringtones.net (down again) - Free Ringtones for your Nokia and Ericsson GSM phone : brought to you by The Ringtone Factory

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you need is the substr function and I think that is where Son was heading.

    $haystack = 'start<LI>middle<LI>ending';
    $match = "<LI>";
    $position_end = substr($haystack, strrpos($haystack, $match), strlen($match));

    One catch this would work but your original example:
    $haystack = 'start<LI>middle<LI>ending<>';
    would just return <> since it is the last occurence of <

    the result of strpos and strrpos is merely a reference to the spot in the haystack where the first or last occurence of needle starts so it will always return a number. What exactly are trying to accomplish it is possible that you may be going about it in the wrong way.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Freddy for pointing out.
    I originally didn't know that strrpos only accepts needle with only ONE char, if you want to use more than 1 char, Freddy's there
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  7. #7
    SitePoint Zealot
    Join Date
    Jun 2000
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need a number returned so it can act as an ending point.

    The complete example would thus be:

    $haystack = 'start<LI>middle<LI>ending<>';

    $position_start = strpos($haystack, "<LI>");
    $position_end = strpos($haystack, "<LI>");

    $haystack = substr($haystack, $position_start, ($position_end - $position_start + 4));

    i.e. very simply, I am trying to cut a piece of the string: '<LI>middle<LI>' but please assume the real string could be much longer. The above example of $haystack just summarises the problem.

    Thanks again guys,

    Jason

  8. #8
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is this?

    $position_start = strpos($haystack, "<LI>");
    $position_end = strpos($haystack, "<LI>");

    These 2 give you the same number!
    Second, as I said and now I quote from php.net:
    Note that the needle in this case can only be a single character. If a string is passed as the needle, then only the first character of that string will be used.
    So i won't use strrpos in your situation.

    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  9. #9
    SitePoint Zealot
    Join Date
    Jun 2000
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah sorry,

    should be:

    $position_start = strpos($haystack, "<LI>");
    $position_end = strrpos($haystack, "<LI>");


    Jason

  10. #10
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jason are you always going to be trying to get something between to tags that are the same like

    somejdkhdjh jk jhkjsf hgjksch klsfg <LI>dhfsjdhf<LI>dkjfh jksdh kjsdh kjsdhfg

    Maybe an ereg would be better

    $haystack = "sdmbhfsdjghfk s dsd jgj fgskjh jkgh jksfhgj sg<LI>xfghsghfishdgfskd hscgksjdgh <LI>fghsj hgsjkdhf kjdhf skjdhf s";
    $needle = "<LI>";
    ereg("$needle(.*)$needle", $haystack, $args);
    print $args[0]."<br>";
    print $args[1]."<br>";

    $agrs[0] would be <LI>dhfsjdhf<LI>
    $args[1] would be dhfsjdhf
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  11. #11
    SitePoint Zealot
    Join Date
    Jun 2000
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Freddy, I'll give it a try.

  12. #12
    SitePoint Enthusiast
    Join Date
    Nov 2000
    Location
    Sydney, Australia
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm okay,

    So was my solution wrong?
    http://www.myringtones.net (down again) - Free Ringtones for your Nokia and Ericsson GSM phone : brought to you by The Ringtone Factory

  13. #13
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not that is was wrong it was just I think we all initially failed to understand clearly what Jason was after.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

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