PHP preg_match_all & Regex

Discussion in 'Programming' started by NaSh123, Oct 19, 2010.

  1. #1
    I am currently using the function below to match all the "hrefs" on the page but I am also trying to grab the text inside the actual url tag

    My Current Code
    
      protected function _get_desc() {
        if (!empty($this->markup)){
          preg_match_all('/<a([^>]+)\>(.*?)\<\/a\>/i', $this->markup, $links);
          return !empty($links[1]) ? $links[1] : FALSE;
        }
      }
    
    PHP:
    I would want to grab the "ShopABC" text.
    
    Sponsored by <a href="/website/6/swp/" target="_blank">ShopABC</a>
    
    HTML:
    I need to modify:
    /<a([^>]+)\>(.*?)\<\/a\>/i

    Any help would be appreciated, I'm stuck!

    Thanks!
     
    NaSh123, Oct 19, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Try this:

    
    protected function _get_desc() {
        if (!empty($this->markup)){
          preg_match_all('/(<a.*>)(.*)(<\/a>)/i', $this->markup, $links, PREG_SET_ORDER);
          return !empty($links[0][2]) ? $links[0][2] : FALSE;
        }
      }
    
    PHP:
     
    s_ruben, Oct 19, 2010 IP