SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Extract href value with PHP

    Hi Guys!

    I need to extract the anchor href content using preg_match_all.

    Does anyone have a link to regex that will do this for me?

    Example content:

    PHP Code:
    <class="class-name here" title="test title" target="_blank" rel="nofollow" onclick="javascript({function_here()})" href="http://sub.test.com/"

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    PHP Code:
    $text='<p>Hello <a class="bla" href="test.php">Test test</a> this is some <a href="bla.html">bla bla</a>, etc, etc</p>';
    preg_match_all('~<a(.*?)href="([^"]+)"(.*?)>~'$text$matches);

    var_dump($matches[2]);

    /*
    array
      0 => string 'test.php' (length=8)
      1 => string 'bla.html' (length=8)
    */ 
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •