views:

85

answers:

4

What is the php code to append class="external" to links that are posted and are not the domain.

For example my site is www.mysite.com and you post a link to www.mysite.com/news and a link to www.yoursite.com

How do I set it so only non "mysite.com" links have the class specified?

+1  A: 

One way is to check the contents of the URL for "http://" (or "https://")

In fact, also check that it does NOT contain your domain name (in case you may have fully-qualified an internal link.)

In PHP, use strpos()

Could also be approached on the client-side using the same general technique. (JQuery is an obvious option since it involves adding a class.)

LesterDove
+1 For jQuery, unless it's critical that's the way to go.
Tim Lytle
This is what I currently have.$this->post['message'] = str_replace('<a href=', '<a class="external" href=', $this->post['message']);$this->post['message'] = str_replace('<a class="external" href="http://thewindows7site.com', '<a href="http://thewindows7site.com', $this->post['message']);$this->post['message'] = str_replace('<a class="external" href="#', '<a href="#', $this->post['message']);return $this->post['message'];It works fine but it is also putting "external" class on images which is a no-no.
K3
@k3 please post this code in your question so its easier for others to read
Rob
wont let me :( too many links error
K3
A: 

could also use regular expressions to test if your domain name exists in the url, if false then its external. the function for php is preg_match()

Rob
+2  A: 

Another approach would be using CSS3 selectors but they are not supported by Internet Explorer 6 (see: http://www.webdevout.net/browser-support-css#css3selectors):

    // external link style
    a:link[href^="http://"] {
        background-color: #990000;
    }   
    // internal link style
    a:link, a:link[href^="http://www.mydomain.com"] {
        background-color: #009900;
        color: #000000;
    }

You would not need to change your links programmatically!

<a href="somelink.htm">some link</a>
<a href="http://www.mydomain.com/anotherlink.htm"&gt;another link</a>

... would result in a link with internal link style.

<a href="http://www.otherdomain.com"&gt;external link</a>

... would result in a link with external link style.

codescape
+1 for elegance and simplicity
graphicdivine
.postbitlegacy .content a:link[href^="http://"] {background:url("images/TW7S_Windows7/External-Links.png") no-repeat scroll right center transparent;padding:0 13px; }This works but it still puts it on pictures (external) as well. :(
K3
A: 

Don't parse html with regular expressions. Really. http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454

Use a DOM parser like this one: http://simplehtmldom.sourceforge.net/

$html = $xxxx //load it from your db or wherever it is
$dom = new simple_html_dom();
$dom->load($html);
foreach($dom->find('a') as $a) {
  $a->class = 'external';
}

done!

Will Shaver