2012-02-10 8 views
-2

私は、いくつかのhtmlコンテンツといくつかのhrefリンクを持つphp変数を持っています。このリンクをキャプチャしてDBに保存し、保存した行のIDに置き換える必要があります(これは、ニュースレターアプリケーションでそのリンクをたどった人の数を追跡するためです)。hrefリンクをキャプチャしてPHPでこれを置き換える方法

基本的には、この例では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. 

+0

[要素のhref属性をつかん]の可能な重複(http://stackoverflow.com/questions/3820666/grabbing-the-href-attribute-of-an-a-element) – Gordon

+1

が削除私の答えは、Gordonが提供したリンクの例が優れています。 – Leigh

答えて

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; 
} 
+0

ありがとう、ありがとう、次の月曜日、私はそれを動作させるためにコードを深く理解するでしょう。 – nmarti

+0

私は私の答えplzをマークするのに役立ちます:) –

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を横断して働きました。

関連する問題