如何捕获href链接并通过PHP替换它

时间:2012-02-10 12:13:18

标签: php hyperlink html-parsing

我有一个带有一些html内容的php变量,以及一些href链接。我需要捕获这些链接,保存到数据库并将其替换为我刚刚保存的行的ID(用于跟踪在简报应用程序中有多少人关注该链接)。

基本上我需要在示例中执行2个函数(some_function_to_save_the_links_to_array和some_function_to_save_the_links_to_array)。

非常感谢你的帮助!

示例:

$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.

2 个答案:

答案 0 :(得分:1)

有多种方法可以做到这一点,但我会这样做,因为你正在遍历HTML。我会帮助你开始,你可以从这里完成它:

$string = "<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>";

$dom = new DOMDocument(); // init 
$dom->loadHTML($string);

$anchors = $DOM->getElementsByTagName('a');
// Traverse anchors
foreach($anchors as $anchor) {
   // Do your magic for saving to the database here
   $anchor->getAttribute('href'); // This would get the value of the href
   $textNode = $anchor->childNodes->item(0)->nodeValue; // This would be the anchor text
   $textNode = 'My new text';
}

如果您复制并粘贴它可能不会起作用,很久以前我曾在PHP中遍历HTML / XML。

答案 1 :(得分: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;
}