2012-04-03 5 views
0

몇 시간 동안 내 머리를 짚어 봤는데, 몇 가지 html을 함수에 전달하고 링크를 대체하고 html을 대체 된 링크로 반환해야합니다.php 대체 링크

<?php 
    final static public function replace_links($campaign_id, $text) { 
     $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>"; 
     if(preg_match_all("/$regexp/siU", $text, $matches, PREG_SET_ORDER)) { 
      foreach($matches as $match) { 
       if(substr($match[2], 0, 2) !== '##') { // ignore the placeholders 
        if(substr($match[2], 0, 6) !== 'mailto') { // ignore email addresses 
         // $match[2] = link address 
         // $match[3] = link text 
         $url = "http://xxx.com/click?campaign_id=$campaign_id&email=##email_address##&next=" . $match[2]; 
         #$text .= str_replace($match[2], $url, $text); 
         #echo $links . "\n"; 
         preg_replace($match[2], "<a href='$url'>{$match[3]}</a>", $match[2]); 
        } 
       } 
       return $text; 
      } 
     } 
    } 
?> 

내가 링크를 표시하면 모든 일치 링크가 표시됩니다. 질문은 아래의 대체 링크 예제를 사용하여 전체 HTML을 반환하는 방법입니다.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<a href="mailto:sssss">xxxx</a> 
<a href="http://www.abc.com/">xxxx</a> 
<a href="http://www.google.com/yeah-baby-yeah">xxxx</a> 
</body> 
</html> 

이 될해야 :이 말이

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<a href="mailto:sssss">xxxx</a> 
<a href="https://xxx.com/click?next=http://www.abc.com/">xxxx</a> 
<a href="https://xxx.com/click?next=http://www.google.com/yeah-baby-yeah">xxxx</a> 
</body> 
</html> 

희망. 사전에

감사합니다,

카일

+0

사용 DOMDocument를하고 링크를 잡기 위해 XPath는, 아주 정직해야한다. – ajreal

+0

고맙습니다 ajreal, 나는 그것을 보았습니다, 나는 보았습니다. http://stackoverflow.com/questions/5317503/php-preg-replace-find-links-and-add-a-hash-to-it –

답변

0

단지 같은 것을 사용하는 바퀴를 재발견하지 마십시오

http://code.google.com/p/jquery-linkify/

그것은 나뿐만 아니라

+0

안녕하세요. Jerome, 이메일을 처리하는 동안 서버 측이어야했습니다. –

+0

아그 난 그게 PHP에 대해 미안해 보지 못했 : /하지만 이미 PHP에 존재하는 무언가를 찾으려고 그때 거기에있을거야;) –

+0

그냥있을 수 있습니다보세요 : http://www.liamdelahunty.com /tips/php_convert_url_to_link.php –

0

를 사용 정말 쉽습니다을 나는 preg_replace에 대해 많이 모른다. 그러나 나는 당신과 똑같은 기능을 필요로했다. 라인 변경 : 이것에 대한

preg_replace($match[2], "<a href='$url'>{$match[3]}</a>", $match[2]); 

을 :

str_replace($match[0],$url,$text); 

트릭을 할 것으로 보인다.

난 그냥 그렇게,이 기능의 반환을 얻을 필요 :

//$text = preg_replace($match[2], "<a href='$url'>{$match[3]}</a>", $match[2]); 
$text = str_replace($match[0],$url,$text); 
+0

죄송합니다. var $ url을 변경해야한다는 사실을 잊어 버렸습니다. 그것과 같은 전체 HTML이되도록 : url as show eletrodomestico

+0

안녕하세요, http://stackoverflow.com/questions/5317503/php-preg-replace-find-links-and-add-a-hash-to-it에서 사용할 수있는 내용을 수정했습니다. . 어쨌든 고마워. –