몇 시간 동안 내 머리를 짚어 봤는데, 몇 가지 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>
희망. 사전에
감사합니다,
카일
사용 DOMDocument를하고 링크를 잡기 위해 XPath는, 아주 정직해야한다. – ajreal
고맙습니다 ajreal, 나는 그것을 보았습니다, 나는 보았습니다. http://stackoverflow.com/questions/5317503/php-preg-replace-find-links-and-add-a-hash-to-it –