2013-02-20 7 views
1

내 기사의 모든 외부 URL을 다시 쓰고 싶지만 nofollowtarget="_blank"을 추가하고 싶습니다. 따라서 외부 사이트에 대한 원래의 링크는 암호화/마스크/재 작성됩니다. 예를 들어기사 내 외부 링크 재 작성/마스크

: rewrite plugin : 외부 링크를 다시 Joomla를위한 플러그인이있다

original link: www.google.com 
rewrite it to: www.mydomain.com?goto=google.com 

.

하지만 저는 joomla를 사용하지 않습니다. 위의 플러그인을 살펴 보시고 내가 찾고있는 것을 정확히 수행합니다.

내가 원하는 것은 무엇입니까?

$article = "hello this is example article I want to replace all external link http://google.com"; 
$host = substr($_SERVER['HTTP_HOST'], 0, 4) == 'www.' ? substr($_SERVER['HTTP_HOST'], 0) : $_SERVER['HTTP_HOST']; 

if (thisIsNotMyWebsite){ 
    replace external url 

} 
+0

링크에서 외부 URL을 바꾸면 어떻게 작동하는지 어떻게 생각하십니까? zip 파일에 연결하면 찾고있는 기능을 더 잘 설명 할 수 없습니다. –

+0

.htaccess – user2051349

+0

을 사용하려고하면 내가 준 링크를 방문하십시오. 여기 데모 http://www.jonijnm.es/web/wiki/66-demo/205-external-links.html –

답변

0

DOMDocument을 사용하여 문서를 구문 분석하고 트래버스 할 수 있습니다.

function rewriteExternal($html) { 

     // The url for your redirection script 
     $prefix = 'http://www.example.com?goto='; 

     // a regular expression to determine if 
     // this link is within your site, edit for your 
     // domain or other needs 
     $is_internal = '/(?:^\/|^\.\.\/)|example\.com/'; 

    $dom = new DOMDocument(); 

    // Parse the HTML into a DOM 
    $dom->loadHTML($html); 

    $links = $dom->getElementsByTagName('a'); 

    foreach ($links as $link) { 
      $href = $link->getAttribute('href'); 
      if (!preg_match($is_internal, $href)) { 
       $link->getAttributeNode('href')->value = $prefix . $href; 
       $link->setAttributeNode(new DOMAttr('rel', 'nofollow')); 
       $link->setAttributeNode(new DOMAttr('target', '_blank')); 
      } 
    } 

    // returns the updated HTML or false if there was an error 
    return $dom->saveHTML(); 
} 

이 방법은 훨씬 더 안정적인 실제로 대신 자주 깨지기 쉬운 정규식에 의존 당신을위한 DOM을 분석하기 때문에 정규 표현식 기반 솔루션을 사용하는 것보다 될 것입니다.

0

뭔가 같은 : 여기 행동

<?php 
$html ='1224 <a href="http://www.google.com">google</a> 567'; 
$tracking_string = 'http://example.com/track.php?url='; 
$html = preg_replace('#(<a[^>]+href=")(http|https)([^>" ]+)("?[^>]*>)#is','\\1'.$tracking_string.'\\2\\3\\4',$html); 
echo $html; 

: http://codepad.viper-7.com/7BYkoc

아니야 자,가요 마지막 업데이트

<?php 
$html =' 1224 <a href="http://www.google.com">google</a> 567'; 

$tracking_string = 'http://example.com/track.php?url='; 
$html = preg_replace('#(<a[^>]+)(href=")(http|https)([^>" ]+)("?[^>]*>)#is','\\1 nofollow target="_blank" \\2'.$tracking_string.'\\3\\4\\5',$html); 

echo $html; 

http://codepad.viper-7.com/JP8sUk

+0

thanks dagon, but you forgot 'nofollow' and target="_blank". I am a newbie so please dont mind –

+0

And also a if condition, If the link is from my own site then dont rewrite them –

+0

would you like a cup of coffee to? –