preg_match_all class

Discussion in 'PHP' started by gilgalbiblewheel, Jul 29, 2009.

  1. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #21
    Didn't work. I got the innerHtml instead of the href:
    preg_match_all('#<a[^>]*class="g-asm"[^>]*>(.+?)</a>#is', $contents_of_page, $classhref, PREG_SET_ORDER);
    foreach ($classhref as $gasmhref)
    {
        echo "Grab: ". $gasmhref[1] ."<br/>";
    }
    PHP:
     
    gilgalbiblewheel, Aug 2, 2009 IP
  2. keaglez

    keaglez Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #22
    LOL, that didn't search the href... I thought you want to learn it yourself so I explained the previous regex. :) And well, we need to know which come first, the class attribute or href attribute before I can help you...
     
    keaglez, Aug 2, 2009 IP
  3. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #23
    Here:
    <a class="g-asm" href="http://something.com/ws/somethingISAPI.dll?ViewItem&amp;ssPageName=STRK:MESOX:IT&amp;item=260447933484" id="ttl_260447933484">something</a>
    Code (markup):
    I would like to know both the full url and then the item # only.
     
    gilgalbiblewheel, Aug 2, 2009 IP
  4. keaglez

    keaglez Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #24
    Here
    
    preg_match_all('#<a[^>]*class="g-asm"[^>]*href="(.+?)"[^>]*>(.+?)</a>#is', $contents_of_page, $classgasm, PREG_SET_ORDER);
    
    PHP:
     
    keaglez, Aug 3, 2009 IP
  5. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #25
    WHen I do print it works fine. But the following:
    preg_match_all('#<a[^>]*class="g-asm"[^>]*href="(.+?)"[^>]*>(.+?)</a>#is', $contents_of_page, $href, PREG_SET_ORDER);
    //print_r($href);
    foreach ($href as $classhref)
    {
        echo "Grab: ". $href[0] ."<br/>";
    }
    PHP:
    Gives:
     
    gilgalbiblewheel, Aug 3, 2009 IP
  6. keaglez

    keaglez Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #26
    LOL.. That because you tried to echo the array...

    in above code, it should be this...
    
    foreach ($href as $classhref)
    {
        echo "Grab: ". $classhref[0] ."<br/>";
        echo "URL: ". $classhref[1] ."<br/>";
        echo "Text: ". $classhref[2] ."<br/>";
    }
    
    PHP:
    But well, you must consistent with your code, in previous code, you pass $classxxx to the preg, but now you reverse it... :)
     
    keaglez, Aug 3, 2009 IP
  7. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #27
    See what happens when I work till late at night?
     
    Last edited: Aug 3, 2009
    gilgalbiblewheel, Aug 3, 2009 IP
  8. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #28
    how do you put each tag in an array?

    I'm looking at a source and everything in the original page is disorganized.

    I was thinking maybe I can grab the tags using regex and put \n at the end to sort it out.

    But I'm not sure how the regex works.
     
    gilgalbiblewheel, Aug 4, 2009 IP