help with preg_match

Discussion in 'PHP' started by ollyno1uk, Aug 4, 2008.

  1. #1
    I'm trying to match a section of a url up until the & symbol within the url

    so say the url is

    www.something.com&src=filename.jpg&size=200

    I'm trying to get just the filename.jpg

    So I try

    preg_match('/www.something.com&src= (.*) &/');

    but it doesn't match it.

    Is it soemthing to do with the &? I have tried \ it but nothing.

    Thanks
     
    ollyno1uk, Aug 4, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Why not use a pattern like:
    /src=(.*?)&/
    Code (markup):
    Jay
     
    jayshah, Aug 4, 2008 IP
  3. ollyno1uk

    ollyno1uk Active Member

    Messages:
    946
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    70
    #3
    ok I am quite a newbie to php - what does the ? mean in the (.*?)

    Thanks for helping
     
    ollyno1uk, Aug 4, 2008 IP
  4. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #4
    It basically means, match everything up until you reach &. (.*) would match everything, ignoring the & and continuing to the end of the string.

    Jay
     
    jayshah, Aug 4, 2008 IP
  5. ollyno1uk

    ollyno1uk Active Member

    Messages:
    946
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    70
    #5
    ok, I understand, but what would define the end of the string with (.*) ? would that mean it would keep searching through the entire page - assuming the url I was matching was within the source of a webpage?

    Can you recommend anywhere that I can find further reading on the topic as searching google for such things as (.*?) etc brings up anything but relevant results?

    Thanks a lot
     
    ollyno1uk, Aug 4, 2008 IP
  6. qprojects

    qprojects Peon

    Messages:
    1,901
    Likes Received:
    103
    Best Answers:
    0
    Trophy Points:
    0
    #6
    www.regular-expressions.info/tutorial.html
    www.codeproject.com/KB/dotnet/regextutorial.aspx
     
    qprojects, Aug 4, 2008 IP
  7. ollyno1uk

    ollyno1uk Active Member

    Messages:
    946
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    70
    #7
    Wow - that is fantastic - thanks a lot
     
    ollyno1uk, Aug 4, 2008 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    nico_swd, Aug 4, 2008 IP
  9. ollyno1uk

    ollyno1uk Active Member

    Messages:
    946
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    70
    #9
    I'm still really stuck with this:

    I managed to get it to pick something - but in the source code there were several instances of

    /src=(.*?)&/
    Code (markup):
    and it just returned a single number. So I went back a bit on the url and found enough info to make it unique however parts of this fell on different lines within the source code and I guess this is causing it not to match.

    So my questions are - what happens if preg_match matches more than one instance within the code and can I limit it to match only the first instance?

    And is it possible to match data that falls over several lines of source code?

    Thanks a lot
     
    ollyno1uk, Aug 5, 2008 IP
  10. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #10
    Use preg_match_all to match more than one, and put 's' after the final / so it becomes /s to match more than one line.

    Jay
     
    jayshah, Aug 5, 2008 IP
  11. ollyno1uk

    ollyno1uk Active Member

    Messages:
    946
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    70
    #11
    ok, so I can't see where I am going wrong with this

    /<div id="product-image"><img src="\/resize.php?subject=product&src=(.*?)&w=200/s
    Code (markup):
    and here is the source:

    
    <div id="product-image">
    					<img src="/resize.php?subject=product&amp;src=b64db4d4c1108080d0d731782fcd6634.jpg&amp;w=200&amp;h=200&amp;type=thumb-full"
    Code (markup):
    and I am trying to get
    b64db4d4c1108080d0d731782fcd6634.jpg
    Code (markup):
    I have tried both
     & and &amp;
    Code (markup):
    and it would seem & is something that at least gets a result

    Am I being totally stupid here and missing something obvious?
     
    ollyno1uk, Aug 5, 2008 IP
  12. ollyno1uk

    ollyno1uk Active Member

    Messages:
    946
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    70
    #12
    just as an update - I was being stupid in other ways - when I was wiritng the data tot he database I was accidentally restricting it to one character.

    Thanks for the help anyways but all sorted
     
    ollyno1uk, Aug 6, 2008 IP