SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular Expression error. HELP!

    Hi all,
    What's wrong with this RE?

    Code:
    if (preg_match ("<a href=http://www.amazon.co.uk/exec/obidos/ASIN/[a-zA-Z0-9]*/ref=br_lf_li_1_2/><font face=verdana, arial, helvetica><b>[a-zA-Z0-9]*</b></font><br></a>", $DataPrint[1])) {
        print "A match was found.";
    } else {
        print "A match was not found.";
    }
    I get the error:-
    Code:
    Unknown modifier '<' in c:\phpdev\www\site\amazon\tets.php on line 37
    Line 37 is the if(preg_match.... line.

    All help appreciated!

  2. #2
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you need to have a delimiter on either side of your pattern. typically, that is a "/". e.g.

    /your_pattern_here/

    but since you have slashes in your pattern, you would have to escape those slashes with backslashes. so, you can use a different delimiter. i use "#" in these cases:

    preg_match('#your_pattern_here#', .....

    and BTW, instead of doing [a-zA-Z] in your pattern, you can just use [a-z] and make the pattern case-insensitive with the "i" modifier. e.g.

    preg_match('#your_pattern_here#i', .....
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  3. #3
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks man, appreciated!

    One more problem though...

    (I'll optimize the string later, but for now...)

    This:
    Code:
    /<a href=http:\/\/www.amazon.co.uk\/exec\/obidos\/ASIN\/[a-zA-Z0-9]*\/gamenewsnetwo-21\/ref=br_lf_li_1_2\/[a-zA-Z0-9]*>/
    Matches this:
    Code:
    <a href=http://www.amazon.co.uk/exec/obidos/ASIN/B00005N5F0/gamenewsnetwo-21/ref=br_lf_li_1_2/>
    But this:
    Code:
    /<a href=http:\/\/www.amazon.co.uk\/exec\/obidos\/ASIN\/[a-zA-Z0-9]*\/gamenewsnetwo-21\/ref=br_lf_li_1_2\/>[a-zA-Z0-9]*<b>/
    Won't match this:
    Code:
    <a href=http://www.amazon.co.uk/exec/obidos/ASIN/B00005N5F0/gamenewsnetwo-21/ref=br_lf_li_1_2/><font="Verdana, Helvetica"><b>
    The two expressions are the same, save for the font tags appended to the end of the query. Why would it not match?

    Any ideas?
    Last edited by KMxRetro; Dec 22, 2001 at 02:58.

  4. #4
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    pattern:

    Code:
    ...ref=br_lf_li_1_2\/>[a-zA-Z0-9]*<b>/
    won't match

    Code:
    ...ref=br_lf_li_1_2/><font="Verdana, Helvetica"><b>
    because the [a-zA-Z0-9] part doesn't match the <, =, ", ,, the space, or > in the bold part above.

    i would change the [a-zA-Z0-9] part to .*?

    that means match ANY character, and the ? means "stop as soon as possible." which means it will only match as much as it has to before it gets to the "<b>" in your HTML string.

  5. #5
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sweet!

    Thanks man, that works a treat.

    I finally got the script to get the link and title of the first product, just need to work out how to get the other 9 from Amazon's "Top Ten" without making the server's processor go nuts...!

    Thanks for you help, it's appreciated.

  6. #6
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no problem. just post if you need any more help.

  7. #7
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, you asked for it....

    The expression works fine now. I'm trying to pull the first TEN matches from the data, so I changed the expression to preg_match_all().

    Only problem is that it's finding the first FIVE, but then ignoring the last 5 in the data. Is there any reason for this?

    Thanks again.

  8. #8
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aha! I know why!

    It seems that some titles have :,(,) and other characters in them, so the expression skips them.

    Is there any way of matching ANY character?

  9. #9
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    . matches any character.
    ck :: bringing chris to the masses.

  10. #10
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Anarchos
    . matches any character.
    yes, but most of the time (and in KMxRetro's case), you want to use .*? with the ? after the quantifier otherwise it will match everything that it can. ? makes it match as LITTLE as possible.

  11. #11
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's the feller I wanted!

    Thanks again Dr Larry! You are a scholar and a gentleman, not to mention a God amongst mere mortals.

    Thanks also Anarchos


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
  •