SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast [Az]'s Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Strange strpos() error

    I'm trying to find the first instance of a double quote after some text but the strpos function seems to want to ignore the character. It then goes back to the beginning of the string and finds the first instance.

    PHP Code:
    <?php 
        $text
    ="<B><font face=\"Verdana\" size=\"2\" color=\"#003366\">New Review @ Hexus!</font></b><br><font face=\"Verdana\" size=\"2\" color=\"#000000\">Posted on Monday, 14 July, 2003 by <a href=\"mailto:Ryszard@hexus.net?subject=Re: New Review @ Hexus!\">Ryszard</a><br><br><span class=\"news\">The mighty Hexus reviewing machine, Tarinder, rolls along with another review, featuring some high performance memory modules from OCZ, their PC3700 EL DDR to be exact.<br><br><i>This review's primary objective was to investigate whether OCZ's impressive claims held true. It's nice when products work just as they were intended to. OCZ's EL DDR PC-3700 Gold memory is about as good as we've seen if high RAM MHz is your thing. The next real performance step, we feel, is to have PC3700 speeds with 2-6-2-2 timings. Is any memory manufacturer brave enough to oblige us ?????</i><br><br>Check it out in full, <a href=\"http://www.hexus.net/review.php?review=579\" target=\"_blank\">here</a>.</span></font><BR><BR><hr noshade width=\"400\" align=\"center\" color=\"#003366\"><!-- end news item -->";
        
    $end_position strpos($text,"\"","review=");
        
        if (
    $end_position === false)
        {
            print 
    "NOT FOUND";
        } else {
            print 
    $end_position;
        }
    ?>
    I've tried converting the double quote to htmlspecialchars and then searching but no joy. Any help appreciated

  2. #2
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What, exactly are you trying to find?
    strpos syntax is: int strpos ( string haystack, string needle [, int offset]).. "review=" is not an integer (-;

    S

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Palo Alto
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by scoates
    What, exactly are you trying to find?
    strpos syntax is: int strpos ( string haystack, string needle [, int offset]).. "review=" is not an integer (-;

    S
    And since "review=" is not an integer, PHP is interpreting it as zero and starting at position zero for its search. I keep thinking there must be a built-in function to do what you want, but if there is I can't remember it. One way to do it is to get your desired start position, and then use that as your offset with strpos:
    PHP Code:
    $offset strpos($text'review=');
    $end_position strpos($text'"'$offset); 
    (Note that inside the single quote marks, you don't need to escape the double quote.)

  4. #4
    SitePoint Enthusiast [Az]'s Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the strpos function defines the offset as the character you want to start searching from, not the integer value

    Unless I'm misereading the section from the PHP manual
    The optional offset parameter allows you to specify which character in haystack to start searching. The position returned is still relative to the the beginning of haystack.
    I am trying to return the id number after the section review.php?review=###

    I initially thought it was the integer value but it seems to work fine if you give it strpos($text,"579","review=")

    I'll give brainpipe's solution a go

  5. #5
    SitePoint Enthusiast [Az]'s Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    looks like I was misreading the (very badly defined) description in the manual as the integer value seems to work now. Must have been a bug with my first attempt at it. Thanks for the help

  6. #6
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have cvs access to the phpdoc tree, what do you think it SHOULD be? (-;

    Seriously, make a suggestion, and I'll change it (or at least run it by the list).

  7. #7
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Palo Alto
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How is it badly defined? It says:
    Code:
    int strpos ( string haystack, string needle [, int offset])
    Which means that strpos takes a string, another string, and then an optional integer; and returns an integer.

  8. #8
    SitePoint Enthusiast [Az]'s Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is contradicted by the comment I quoted as it isn't clear that it means the integer value. Granted, the top line does specify an integer but I wasn't getting the results I'd expected using integers. Once you've spent 4 hours trying to debug something it doesn't help if there's no consistency across the manual and with the sweltering heat here my mind was beginning to dissolve

    Anyway, it would be useful if the statement was

    "The optional offset parameter allows you to specify the integer position in haystack to start searching from. The position returned is still relative to the the beginning of haystack."

  9. #9
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think that's MORE confusing. (-:

    abcdef

    the third character position contains C.

    (shrug)

    S


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
  •