Justtechjobs.com Find a programming school near you






Online Campus Both


php-general | 2001092

[PHP] Re: regular expression help From: _lallous (elias_bachaalany <email protected>)
Date: 09/18/01

This should do (but ofcourse you might want to play a bit with it)

$mem =
'
<A HREF="http://www.mydomain.com/mypage.php">something</a> is fine
<A HREF="http://www.yourdomain.com/yourpage.php">something</a> is wrong
<A HREF="http://www.yourdomain.com/yourpage.php">something</a> is fine<br>
<a href="http://www.lgwm.org/">lgwm</a><br>
<a href="http://www.google.com"
onclick="javascript:alert(\'hello\');">Google!</a>
';

function handler($theTag, $theDomain)
{
  if (!strstr($theDomain, "mydomain.com"))
  {
    $theTag = strstr($theTag, " ");
    $theTag = "<a target='_blank'$theTag";
  }
  return stripslashes($theTag);
}

$re = "/<\s*a\s*href\s*=\s*(['\"])(.+?)\\1[^>]*>/eis";
$t = preg_replace($re, "handler('\\0', '\\2')" ,$mem);
echo $t;

//greetings R&Ze!

"Justin French" <justin <email protected>> wrote in message
news:3BA68E3F.BF7C7C08 <email protected>
> hi all,
>
> I have some user-supplied text on a content driven site.
> I am allowing <A> tags inside the main text, BUT I want
> any links to external sites (not on the chosen domain)
> to include a ' TARGET="_new" ' element.
>
> So,
>
> <A HREF="http://www.mydomain.com/mypage.php">something</a> is fine
> <A HREF="http://www.yourdomain.com/yourpage.php">something</a> is wrong
> <A HREF="http://www.yourdomain.com/yourpage.php"
> TARGET="_new">something</a> is fine
>
> And of course there are all the variants with and without
> the www, and with and without sub directories and pages.
>
> I'[d also like to make sure the dopey people have put a
> close tag in.
>
>
> I've got a few ideas on how it might be done, but I need
> to do it the right way, avoiding slight human errors etc.
>
>
> Has anyone got something written, or can point me in the
> right direction?
>
>
> Justin French

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe <email protected>
For additional commands, e-mail: php-general-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>