1

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?

K3.
  • 13
  • 1
  • 3
  • Can you post your the code with wich you show the links? Another good solution is to use JavaScript and parse the dom for links check if they are your domain and if not add the appropriate class="external" – Ivo Sabev Apr 08 '10 at 13:52
  • @Ivo Sabev, problem with javascript solutions are they can be disabled in the browser, so the style wouldn't be applied in all cases. – studioromeo Apr 08 '10 at 13:58
  • @Rob If he doesn't care about that it is the easiest solution. Others depend on his PHP code which if we cannot see we cannot say anything. – Ivo Sabev Apr 08 '10 at 14:09
  • The commenting system here is different. I posted my reponse below. – K3. Apr 08 '10 at 14:14

4 Answers4

4

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">another link</a>

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

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

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

stefanglase
  • 10,296
  • 4
  • 32
  • 42
  • .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. Apr 08 '10 at 14:30
1

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
  • 3,014
  • 1
  • 23
  • 24
1

Don't parse html with regular expressions. Really. RegEx match open tags except XHTML self-contained tags

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!

Community
  • 1
  • 1
Will Shaver
  • 12,471
  • 5
  • 49
  • 64
0

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()

studioromeo
  • 1,563
  • 12
  • 18