SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regex and getting contents from a link?

    Hey guys, I need to grab the link out of a link;

    <a href="www.google.com">Google</a>

    Needs to be

    1) www.google.com
    2)Google

    I'm not hot on regex and Ive left all my docs that I wrote as a help line at home

    Thanks in advance
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Your link is invalid HTML.

    <a href="http://www.google.com">Google</a>
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by siteguru View Post
    Your link is invalid HTML.

    <a href="http://www.google.com">Google</a>
    You know what I meant
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  4. #4
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ive managed to do this without regex, like this:

    PHP Code:

    $content 
    "<a href='http://www.google.com'>Google</a>";

    $linkparts explode('"'$content);
    $mainlink $linkparts[1];
    $linknametemp $linkparts[2];
    $linknametemp2 str_replace('>'''$linknametemp);
    $linkname str_replace('</a>'''$linknametemp2); 
    Is this the most efficient way of doing this though?
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  5. #5
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    /\<a\b[^>]*href=[\'\"]([^\'\"]+)[\'\"][^>]*\>((?:(?!\<\/a\>).)+)/is

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by bokehman View Post
    Code:
    /\<a\b[^>]*href=[\'\"]([^\'\"]+)[\'\"][^>]*\>((?:(?!\<\/a\>).)+)/is
    nice
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the pattern Bokemon, works a treat Any chance of a little explanation as to how it works?

    Another problem I have is the code I have produced only works on the first link it comes to and then stop, my understanding is that it should look through the entire content and do what it needs to do for the everything it finds with that pattern, here is the code I'm using:

    PHP Code:
    function counturl($content) {

        include 
    "db.php";
        
        
    preg_match('/\<a\b[^>]*href=[\'\"]([^\'\"]+)[\'\"][^>]*\>((?:(?!\<\/a\>).)+)/is'$content$link);
        
        
    $linkname str_replace("'"""$link[2]);
        
    $mainlink mysql_real_escape_string(str_replace('http://'''$link[1]));
        
        
    $result_linkcheck mysql_query("SELECT * FROM tabLinkcounter WHERE linLink='$mainlink'") or die(mysql_error());
        
        if (
    mysql_num_rows($result_linkcheck) == 0) {
            
            
    $result_insertlink mysql_query("INSERT INTO tabLinkcounter SET linName='$linkname', linLink='$mainlink', linCount=0") or die(mysql_error());
        }
        
        echo 
    str_replace("http://""http://www.silversave.co.uk?userlink="$content);
        

    What I am trying to do is grab any content that the user has entered into the DB, check it for links and add each link to a table for counting the clicks of the link, if its already in the DB it wont overwrite it. This works fine but will only add the first link it comes across in the content and ignores the rest.

    Any ideas ?

    Thanks again
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"

  8. #8
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webnoob View Post
    Any chance of a little explanation as to how it works?
    If you want to learn about patterns have a look at http://regexp.info/

    Quote Originally Posted by webnoob View Post
    only works on the first link it comes to and then stop
    use preg_match_all

  9. #9
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that bokemon, much appreciated.
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"


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
  •