2016-07-16 77 views
3

我有一個html代碼。現在我想查找所有的href標籤,並希望將其url轉換爲另一個url。我使用這個代碼查找並將所有href網址轉換爲另一個網址php

public function convertLinks($message,$stats_code) 
{ 
    $click_link = site_url('click-'.$stats_code.'email'); 
    $content = explode("\n", $message); 
    $URLs = array(); 
    for($i=0;count($content)>$i;$i++) 
    { 
    if(preg_match('/<a href=/', $content[$i])) 
     { 
     list($Gone,$Keep) = explode("href=\"", trim($content[$i])); 
     list($Keep,$Gone) = explode("\">", $Keep); 
     $message= strtr($message, array("$Keep" => $click_link.$Keep,)); 
     } 
    } 
    return $message; 
} 

此代碼工作正常,但在2個或更多的href鏈接是一條線,只轉換第一HREF鏈接,也當ancher標籤還具有其它任何標籤像類或目標或任何除了無法轉換鏈接。 請給我合適的解決方案 預先感謝您

回答

1
public function convertLinks($message,$stats_code) 
{ 
    $click_link = site_url('click-'.$stats_code.'email'); 
    $message = str_replace('<a','{smart_href_link_epro} <a',$message); 
    $content = explode("{smart_href_link_epro}", $message); 
    $a_count = count($content); 

    for($i=0;$a_count>$i;$i++) 
    { 
    if(preg_match('/href=/', $content[$i])) 
     { 
     list($Lost,$Keep) = explode("href=\"", trim($content[$i])); 
     list($Keep,$Lost) = explode("\"", $Keep); 
     if($Keep!='#' && $Keep!='') 
     $message= strtr($message, array("$Keep" => $click_link.base64_encode(base64_encode($Keep)),)); 
     } 
    } 

    return str_replace('{smart_href_link_epro}','',$message); 
} 

它爲我perfactly ..

相關問題