-2

I have a php variable with some html content, and some href links. I need to capture this links, save to a DB and replace it with the id of the row that I just saved (is to track how many people follows that link in a newsletter application).

Basicatly I need to do the 2 functions in the example (some_function_to_save_the_links_to_array and some_function_to_save_the_links_to_array).

Thank you a lot for your help!

Example:

$var = "<html><body><h1>This is the newsletter</h1><p>Here I have <a href='http://www.google.com'>Some links</a> in the body of this <a href='http://www.yahoo.com'>Newsletter</a> and I want to extract their.</body></html>";
//Here I just no know how to do this, but I need to save http://www.google.com and http://www.yahoo.com to, maybe, an array, and save this array in a mysql db.
some_function_to_save_the_links_to_array;
while (THERE ARE VALUES IN THE ARRAY OF THE LINKS){
 save $array['X'] to db //(I already know how to do it, this is not the problem)
 $id = last inserted row in db //I know how to do it also
 function_to_replace_the_links_for_the_id;
}
echo $var;

And this is the echo:
<html><body><h1>This is the newsletter</h1><p>Here I have <a href='http://www.mysite.com/link.php?id=1'>Some links</a> in the body of this <a href='http://www.mysite.com/link.php?id=1'>Newsletter</a> and I want to extract their.

Gordon
  • 312,688
  • 75
  • 539
  • 559
nmarti
  • 1,565
  • 3
  • 14
  • 19
  • possible duplicate of [Grabbing the href attribute of an A element](http://stackoverflow.com/questions/3820666/grabbing-the-href-attribute-of-an-a-element) – Gordon Feb 10 '12 at 12:21
  • 1
    deleted my answer, the example in the link Gordon provided is superior. – Leigh Feb 10 '12 at 12:25

1 Answers1

1
<?php
function captureLink($content) 
{
    $links = array();
    $pattern = "/<a\s+href=[\"\']([^>]+?)[\"\']/iU";
    if(preg_match_all($pattern,$content,$matches)) {
        for($i = 0;$link = $matches[$i][1];$i++)
            array_push($links,$link);
    }
    return $links;
}

function insertLinksToDb(array $links)
{
    $linksDb = array();
    foreach($links as $link) {
        $hash_link = md5($link);
        $sql = "SELECT (id) FROM links WHERE hash LIKE :hash";
        $sth = $dbh->prepare($sql);
        $sth->bindValue(':hash',$hash_link,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);
        if(!empty($result)) {
            $id = $result['id'];
            $linksDb[$id] = $link;
        } else {
            $sql = " INSERT INTO links (hash,link) VALUES(:hash,:link);";
            $sth = $dbh->prepare($sql);
            $sth->execute(array(':hash'=>$hash_link,':link',$link));
            $linksDb[PDO::lastInsertId] = $link;
        }
    }
    return $linksDb;
}

function normallizeLinks($content,array $links)
{
    foreach($links as $id => $link) {
        //str_replace working faster than strtr
        $content = str_replace($link,'/links.php?id='.$id,$content);
    }
    return $content;
}
v.tsurka
  • 452
  • 6
  • 18