Link to home
Start Free TrialLog in
Avatar of ellandrd
ellandrdFlag for Ireland

asked on

change attribute values for <a></a> tags

i have managed with the help of some experts to take a basic <a></a> tag and create a custom <a></a> tag.

sample text:

$str = '<p>This is a link to the <a href="http://www.google.ie/" title="Google Ireland Homepage">Google Ireland</a> homepage.</p>';

after I run my set of functions below on the above sample text above I get:

<p>This is a link to the <a onclick="javascript:window.open(this.href);return false;" rel="nofollow" href="http://www.google.ie/" title="Google">http://www.google.ie/</a> <img src="external.gif" alt="This is an external link" /> homepage.</p>

However, notice that the original value between <a></a> tags was "Google" and is now replaced by the URL: http://www.google.ie/

How can I keep the original value of the <a></a> tags, if it is possible?

Ellandrd
<?php
function fcnFixURLTitleAttribute($data) 
{
	preg_match_all('/<a[^>]+>(.*?)<\/a>/',$data,$titles);
 
	if(count($titles[1]) > 0) 
	{
		foreach($titles[1] as $title) 
		{
			$title = str_replace('https://','http://',$title); 
 
			if(!preg_match("/http/i",$title)) 
			{
				$title = 'http://'.$title;
			}
			
			if(preg_match("/www/i",$title)) 
			{
				$title = str_replace('http://','',$title);
				$title = str_replace('https://','',$title); 
				$title = str_replace('www','http://www',$title); 
			}
 
			$fp = $contents = '';
 
			if(($fp = fopen($title,'r')) === false) 
			{
				$data = preg_replace('/title=""/','title="'.htmlentities(strip_tags($title)).'"',$data,1);
			} 
			else 
			{
				while(!feof($fp)) 
				{
					$contents .= fgets($fp,1024);
					
					if(stristr($contents,'</title>')) 
					{
						break;
					}
				}
				
				fclose($fp);
	
				if(eregi("<title>(.*)</title>",$contents,$out)) 
				{
					$data = preg_replace('/title=""/','title="'.htmlentities(strip_tags($out[1])).'"',$data,1);
				} 
				else 
				{
					$data = preg_replace('/title=""/','title="'.htmlentities(strip_tags($title)).'"',$data,1);
				}
			}
		}
	}
	
	$data = str_replace('http://','',$data); 
	$data = str_replace('href="','href="http://',$data);
	
	unset($titles,$fp,$contents);
 
	return $data;
}
 
function fcnConvertWebsiteAddress($data) 
{
	$data = preg_replace('/(https?\:\/\/\S*)/','<a onclick="javascript:window.open(this.href);return false;" rel="nofollow" href="${1}" title="">${1}</a> <img src="/media/external.gif" title="This is an external link." alt="This is an external link.">',$data);
		
	return preg_replace('/(?<![\'"])([^\/])(www\.[^\s\,"<]+)/','$1<a onclick="javascript:window.open(this.href);return false;" rel="nofollow" href="${2}" title="">${2}</a> <img src="/media/external.gif" title="This is an external link." alt="This is an external link.">',$data);
}
 
function fcnStripWebsiteAddress($str)
{
	$str = preg_replace('@<a\s+.*href="(.*)".*>.*</a>@iU','$1',$str);
 
	return $str;
}
 
function fcnCheckForExternalURL($data) 
{
	$pattern = $replace = '';
 
	preg_match_all('/<a[^>]+>(.*?)<\/a>/',$data,$addresses);
 
	if(count($addresses[1]) > 0) 
	{
		foreach($addresses[1] as $address) 
		{
			if(preg_match("/http/i",$address)) 
			{
				$pattern = '/onclick=".*" rel="nofollow" (href\="https?\:\/\/(seandelaney.co.uk[\/"]))/';
			} 
			else 
			{
				$pattern = '/onclick=".*" rel="nofollow" (href\="https?\:\/\/www\.?seandelaney.co.uk\/?")/';
			}
			
			if(preg_match("/www/i",$address)) 
			{
				$pattern = '/onclick=".*" rel="nofollow" (href\="www\.?seandelaney.co.uk\/?")/';
			}
 
			$replace = '${1}';
			$data = preg_replace($pattern,$replace,$data);
		}
	}
 
	unset($pattern,$replace,$addresses);
	
	return $data;
}	
 
function fcnDoHouseCleaning($data)
{
	$data = str_replace("'","&#39;",$data);
	$data = str_replace("\'","&#39;",$data);
	$data = str_replace('\"','"',$data);
 
	return trim($data);
}
 
$str = '<p>This is a link to the <a href="http://www.google.ie/" title="Google Ireland Homepage">Google Ireland</a> homepage.</p>';
 
$str = fcnDoHouseCleaning($str);
$str = fcnStripWebsiteAddress($str);
$str = fcnConvertWebsiteAddress($str);
$str = fcnCheckForExternalURL($str);
$str = fcnFixURLTitleAttribute($str);
 
$str = str_replace('www','http://www',$str); 
$str = str_replace('http://http://','http://',$str);
 
echo $str; 
?>

Open in new window

Avatar of mostart
mostart

try this:
<?php
 
 
function fcnFixURLTitleAttribute($data)
{
        preg_match_all('/<a[^>]+>(.*?)<\/a>/',$data,$titles);
 
        if(count($titles[1]) > 0)
        {
                foreach($titles[1] as $title)
                {
                        $title = str_replace('https://','http://',$title);
 
                        if(!preg_match("/http/i",$title))
                        {
                                $title = 'http://'.$title;
                        }
 
                        if(preg_match("/www/i",$title))
                        {
                                $title = str_replace('http://','',$title);
                                $title = str_replace('https://','',$title);
                                $title = str_replace('www','http://www',$title);
                        }
 
                        $fp = $contents = '';
 
                        if(($fp = fopen($title,'r')) === false)
                        {
                                $data = preg_replace('/title=""/','title="'.htmlentities(strip_tags($title)).'"',$data,1);
                        }
                        else
                        {
                                while(!feof($fp))
                                {
                                        $contents .= fgets($fp,1024);
 
                                        if(stristr($contents,'</title>'))
                                        {
                                                break;
                                        }
                                }
 
                                fclose($fp);
 
                                if(eregi("<title>(.*)</title>",$contents,$out))
                                {
                                        $data = preg_replace('/title=""/','title="'.htmlentities(strip_tags($out[1])).'"',$data,1);
                                }
                                else
                                {
                                        $data = preg_replace('/title=""/','title="'.htmlentities(strip_tags($title)).'"',$data,1);
                                }
                        }
                }
        }
 
        $data = str_replace('http://','',$data);
        $data = str_replace('href="','href="http://',$data);
 
        unset($titles,$fp,$contents);
 
        return $data;
}
 
function fcnConvertWebsiteAddress($data)
{
        $data = preg_replace('/(https?\:\/\/\S*)/','<a onclick="javascript:window.open(this.href);return false;" rel="nofollow" href="${1}" title="">${1}</a> <img src="/media/external.gif" title="This is an extern>al link." alt="This is an external link.">',$data);
 
        return preg_replace('/(?<![\'"])([^\/])(www\.[^\s\,"<]+)/','$1<a onclick="javascript:window.open(this.href);return false;" rel="nofollow" href="${2}" title="">${2}</a> <img src="/media/external.gif" title=>"This is an external link." alt="This is an external link.">',$data);
}
 
function fcnGetLinkName ($str) {
        $str = preg_replace('@.*<a\s+.*href=".*".*>(.*)</a>.*@','$1',$str);
 
        return $str;
}
 
function fcnStripWebsiteAddress($str)
{
        $str = preg_replace('@<a\s+.*href="(.*)".*>.*</a>@iU','$1',$str);
 
        return $str;
}
 
function fcnCheckForExternalURL($data)
{
        $pattern = $replace = '';
 
        preg_match_all('/<a[^>]+>(.*?)<\/a>/',$data,$addresses);
 
        if(count($addresses[1]) > 0)
        {
                foreach($addresses[1] as $address)
                {
                        if(preg_match("/http/i",$address))
                        {
                                $pattern = '/onclick=".*" rel="nofollow" (href\="https?\:\/\/(seandelaney.co.uk[\/"]))/';
                        }
                        else
                        {
                                $pattern = '/onclick=".*" rel="nofollow" (href\="https?\:\/\/www\.?seandelaney.co.uk\/?")/';
                        }
 
                        if(preg_match("/www/i",$address))
                        {
                                $pattern = '/onclick=".*" rel="nofollow" (href\="www\.?seandelaney.co.uk\/?")/';
                        }
 
                        $replace = '${1}';
                        $data = preg_replace($pattern,$replace,$data);
                }
        }
 
        unset($pattern,$replace,$addresses);
 
        return $data;
}
 
function fcnDoHouseCleaning($data)
{
        $data = str_replace("'","&#39;",$data);
        $data = str_replace("\'","&#39;",$data);
        $data = str_replace('\"','"',$data);
 
        return trim($data);
}
 
$str = '<p>This is a link to the <a href="http://www.google.ie/" title="Google Ireland Homepage">Google Ireland</a> homepage.</p>';
 
$str = fcnDoHouseCleaning($str);
 
$linkName = fcnGetLinkName($str);
 
$str = fcnStripWebsiteAddress($str);
$str = fcnConvertWebsiteAddress($str);
$str = fcnCheckForExternalURL($str);
$str = fcnFixURLTitleAttribute($str);
 
$str = str_replace('www','http://www',$str);
$str = str_replace('http://http://','http://',$str);
 
$str = preg_replace('@(<a\s+.*href=".*".*>)(.*)(</a>)@','$1'.$linkName.'$3',$str);
 
echo $str ;
?>

Open in new window

Avatar of ellandrd

ASKER

Hi

The fcnGetLinkName only keeps the value for the last href in the $str.  If I have 1 <a></a> everything works fine but if I have 2 or 3 <a></a> tags only the last set of <a></a> tags keep original <a></a > values?

My test:

<p>this is <a onclick="javascript:window.open(this.href);return false;" rel="nofollow" href="http://www.seandelaney.co.uk" title="Freelance Web Design">http://www.seandelaney.co.uk</a> <img src="/media/external.gif" title="This is an extern>al link." alt="This is an external link."> and this is <a onclick="javascript:window.open(this.href);return false;" rel="nofollow" href="http://www.velodays.com" title="Velodays Cycling Holidays - Cycling Holidays &amp; Short Breaks In Scotland">link2</a> <img src="/media/external.gif" title="This is an extern>al link." alt="This is an external link."> and i am testing if the custom link tool is working?</p>
ASKER CERTIFIED SOLUTION
Avatar of ellandrd
ellandrd
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
could you share your solution then ?
function fcnStripWebsiteAddress($str)
{
      return preg_replace('@<a\s+.*href="(.*)".*>.*</a>@iU','$1',$str);
}
hmm.. but this function is exactly what you had before (you can find it in my post already) ? So where is your solution then ?
sorry... posted wrong function....

this is the one i changed to get it working...


function fcnConvertWebsiteAddress($data)
{
      //preg_replace('@<a\s+.*href="(.*)".*>(.*)</a>@iU','{$1}',$data);
      
      //if(preg_match('/holidays/i',$data))
      //{
      //      return $data;
      //}
      //else
      //{
            return preg_replace('@<a\s+.*href="(.*)".*>(.*)</a>@iU','<a onclick="javascript:window.open(this.href);return false;" rel="nofollow" href="${1}" title="">${2}</a> <img src="/media/external.gif" title="This is an external link." alt="This is an external link.">',$data);
      //}
}