+ Reply to Thread
Results 1 to 7 of 7

Thread: PHP script - adding target="_blank" to links

  1. #1
    booksgo is offline x10 Sophmore booksgo is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    109

    PHP script - adding target="_blank" to links

    Can anyone help with adding <rel="nofollow" target="_blank"> to the code for the 'link' below? I tried adding it in various places, without success. Thanks for any help.

    Code:
    <?php
    /*
    	RSS Extractor and Displayer
    	(c) 2007  Scriptol.com - Licence Mozilla 1.1.
    	rsslib.php
    	
    	Requirements:
    	- PHP 5.
    	- A RSS feed.
    	
    	Using the library:
    	Insert this code into the page that displays the RSS feed:
    	
    	<?php
    	require_once("rsslib.php");
    	echo RSS_Display("http://www.xul.fr/rss.xml", 25);
    	?>
    	
    */
    
    $RSS_Content = array();
    
    function RSS_Tags($item, $type)
    {
    		$y = array();
    		$tnl = $item->getElementsByTagName("title");
    		$tnl = $tnl->item(0);
    		$title = $tnl->firstChild->data;
    
    		$tnl = $item->getElementsByTagName("link");
    		$tnl = $tnl->item(0);
    		$link = $tnl->firstChild->data;
    
    		$tnl = $item->getElementsByTagName("description");
    		$tnl = $tnl->item(0);
    		$description = $tnl->firstChild->data;
    
    		$y["title"] = $title;
    		$y["link"] = $link;
    		$y["description"] = $description;
    		$y["type"] = $type;
    		
    		return $y;
    }
    
    
    function RSS_Channel($channel)
    {
    	global $RSS_Content;
    
    	$items = $channel->getElementsByTagName("item");
    	
    	// Processing channel
    	
    	$y = RSS_Tags($channel, 0);		// get description of channel, type 0
    	array_push($RSS_Content, $y);
    	
    	// Processing articles
    	
    	foreach($items as $item)
    	{
    		$y = RSS_Tags($item, 1);	// get description of article, type 1
    		array_push($RSS_Content, $y);
    	}
    }
    
    function RSS_Retrieve($url)
    {
    	global $RSS_Content;
    
    	$doc  = new DOMDocument();
    	$doc->load($url);
    
    	$channels = $doc->getElementsByTagName("channel");
    	
    	$RSS_Content = array();
    	
    	foreach($channels as $channel)
    	{
    		 RSS_Channel($channel);
    	}
    	
    }
    
    
    function RSS_RetrieveLinks($url)
    {
    	global $RSS_Content;
    
    	$doc  = new DOMDocument();
    	$doc->load($url);
    
    	$channels = $doc->getElementsByTagName("channel");
    	
    	$RSS_Content = array();
    	
    	foreach($channels as $channel)
    	{
    		$items = $channel->getElementsByTagName("item");
    		foreach($items as $item)
    		{
    			$y = RSS_Tags($item, 1);	// get description of article, type 1
    			array_push($RSS_Content, $y);
    		}
    		 
    	}
    
    }
    
    
    function RSS_Links($url, $size)
    {
    	global $RSS_Content;
    
    	$page = "<ul>";
    
    	RSS_RetrieveLinks($url);
    	if($size > 0)
    		$recents = array_slice($RSS_Content, 0, $size);
    
    	foreach($recents as $article)
    	{
    		$type = $article["type"];
    		if($type == 0) continue;
    		$title = $article["title"];
    		$link = $article["link"];
    		$page .= "<li><a href=\"$link\">$title</a></li>\n";			
    	}
    
    	$page .="</ul>\n";
    
    	return $page;
    	
    }
    
    
    
    function RSS_Display($url, $size)
    {
    	global $RSS_Content;
    
    	$opened = false;
    	$page = "";
    
    	RSS_Retrieve($url);
    	if($size > 0)
    		$recents = array_slice($RSS_Content, 0, $size);
    
    	foreach($recents as $article)
    	{
    		$type = $article["type"];
    		if($type == 0)
    		{
    			if($opened == true)
    			{
    				$page .="</ul>\n";
    				$opened = false;
    			}
    			$page .="<b>";
    		}
    		else
    		{
    			if($opened == false) 
    			{
    				$page .= "<ul>\n";
    				$opened = true;
    			}
    		}
    		$title = $article["title"];
    		$link = $article["link"];
    		$description = $article["description"];
    		$page .= "<li><a href=\"$link\">$title</a>";
    		if($description != false)
    		{
    			$page .= "<br>$description";
    		}
    		$page .= "</li>\n";			
    		
    		if($type==0)
    		{
    			$page .="</b><br />";
    		}
    
    	}
    
    	if($opened == true)
    	{	
    		$page .="</ul>\n";
    	}
    	return $page."\n";
    	
    }
    
    
    ?>
    Last edited by booksgo; 12-17-2008 at 11:27 AM. Reason: Added [code] tags
    -------------------------------------

    -------------------------------------

  2. #2
    Scoochi2's Avatar
    Scoochi2 is offline x10 Sophmore Scoochi2 is an unknown quantity at this point
    Join Date
    Aug 2008
    Location
    Southport!
    Posts
    185

    Re: PHP script - adding target="_blank" to links

    Copy all that into a simple notepad program or equivalent.
    Use search+replace and replace;
    <a href=
    with
    <a rel='nofollow' target='_blank' href=
    If anyone can see it, my post was meant for anyone who reads it. Don't take it personally or think I'm being condescending... :nuts:

  3. #3
    vol7ron's Avatar
    vol7ron is offline x10 Lieutenant vol7ron is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    DC
    Posts
    434

    Re: PHP script - adding target="_blank" to links

    It is wrong to suggest using target="". W3 has deprecated this tag and it does not conform to strict w3 standards.

    One solutions is to:
    <a href="http://forums.x10hosting.com/programming-help/yourpage.html" onclick="window.open('yourpage.html');return false;">

    Another alternative that I like to do is change the links using Javascript on the window load. I give each link I want to change a class of "updateLink." Note: if you set more than one class to your links, then you'll have to use a regular expression. Setting the target, using Javascript is perfectly acceptable as W3 specification does not cover DOM control, which JS is.

    Code:
       for (var e in document.links)
          if (document.links[e].className == "updateLink")
             document.links[e].target="_blank";
    The problem with this is that users may have JS turned off. The result will be that the link will open in the same browser window as the page, which is okay because you still have the back button to work with.
    Last edited by vol7ron; 12-17-2008 at 09:04 AM.
    If you find my post useful please add to my reputation by clicking the +Rep button
    You may also use the Donate link to donate credits - this is appreciated too Thanks to those whom have donated so far!


  4. #4
    booksgo is offline x10 Sophmore booksgo is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    109

    Re: PHP script - adding target="_blank" to links

    Quote Originally Posted by Scoochi2 View Post
    Copy all that into a simple notepad program or equivalent.
    Use search+replace and replace;
    <a href=
    with
    <a rel='nofollow' target='_blank' href=

    It works! Thanks for your help.
    Edit:
    Quote Originally Posted by vol7ron View Post
    It is wrong to suggest using target="". W3 has deprecated this tag and it does not conform to strict w3 standards.
    It's OK for Transitional though, and it works so thats good enough for me ;)
    Last edited by booksgo; 12-17-2008 at 09:58 AM. Reason: Automerged Doublepost
    -------------------------------------

    -------------------------------------

  5. #5
    vol7ron's Avatar
    vol7ron is offline x10 Lieutenant vol7ron is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    DC
    Posts
    434

    Re: PHP script - adding target="_blank" to links

    It will work with transitional, but so does align="center" - it doesn't mean you should use it. Transitional should only be used when there is no other choice, but I provided one.

    Every page should try to adhere to strict standards as it will not only improve search engine rankings, but the fact will be that transitional will one day be dropped and you'll be forced to reason with strict anyhow.
    Last edited by vol7ron; 12-17-2008 at 10:02 AM.
    If you find my post useful please add to my reputation by clicking the +Rep button
    You may also use the Donate link to donate credits - this is appreciated too Thanks to those whom have donated so far!


  6. #6
    booksgo is offline x10 Sophmore booksgo is an unknown quantity at this point
    Join Date
    Apr 2008
    Posts
    109

    Re: PHP script - adding target="_blank" to links

    I know you are right really, but at the moment doing things to Transitional is hard enough for me :-)
    -------------------------------------

    -------------------------------------

  7. #7
    vol7ron's Avatar
    vol7ron is offline x10 Lieutenant vol7ron is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    DC
    Posts
    434

    Re: PHP script - adding target="_blank" to links

    all have to start somewhere
    If you find my post useful please add to my reputation by clicking the +Rep button
    You may also use the Donate link to donate credits - this is appreciated too Thanks to those whom have donated so far!


+ Reply to Thread

Similar Threads

  1. PHP Versions & Resource Usage Script
    By Bryon in forum News and Announcements
    Replies: 11
    Last Post: 02-27-2008, 05:51 PM
  2. Unstand PHP?
    By o0slowpaul0o in forum Tutorials
    Replies: 8
    Last Post: 01-07-2008, 09:16 PM
  3. 500 credits for php script
    By Christopher in forum The Marketplace
    Replies: 3
    Last Post: 10-19-2007, 11:47 PM
  4. PHP script not working (RSSgenr8.php)
    By deepwater in forum Scripts & 3rd Party Apps
    Replies: 0
    Last Post: 09-21-2007, 09:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers