title: Adding _blank targets to external links
body: <p>I just want to know how can I add a _blank target type to a link if a link is pointing to an external domain (_self for internals). I was doing this by checking the url but it was really hard coded and not reusable for other sites.</p>
  
  <p>Do you have any idea of how to do it properly with PHP ?</p>
  
  <pre><code>$target_type=(strpos($ref, $_SERVER['HTTP_HOST'])&gt;-1 
  || strpos($ref,'/')===0? '_self' : '_blank');
  
  if ($ref&lt;&gt;'#' &amp;&amp; substr($ref,0,4)&lt;&gt;'http') $ref='http://'.$ref;
  $array['href']=$ref;
  if (substr($ref,0,1)&lt;&gt;'#') $array['target']= $target_type;
  $array['rel']='nofollow';
  if (empty($array['text'])) $array['text']=str_replace('http://','',$ref);
  </code></pre>
  
  <p>This is only working for the main domain, but when using <em>domain.com/friendlyurl/</em>, is not working.</p>
  
  <p>Thanks in advance</p>
  
  <p>NOTE : Links can contain whether http:// protocol or not and they use to be absolute links. Links are added by users in the system</p>