Php & Xml

Discussion in 'PHP' started by AT-XE, Mar 26, 2008.

  1. #1
    Hello guys. I have a php script which is:

    <?php
    $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");
    	
    	foreach($items as $item)
    	{
    		$y = RSS_Tags($item, 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 = "";
    
    	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";
    	
    }
    
    
    ?>
    PHP:
    and I was wondering if I could with any way to cut the rss images from the page.

    Thank you,
    AT-XE.
     
    AT-XE, Mar 26, 2008 IP
  2. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I am paying $5 now if someone answers me and helps me to solve this.
     
    AT-XE, Mar 27, 2008 IP
  3. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #3
    I presume the script you are using is an RSS parser. Instead of building into the script a function to include images why don't you try a different RSS parser which already retrieves images such as;

    http://magpierss.sourceforge.net/

    Alternatively you need to create a few lines of code in the function RSS_Tags() to retrieve the appropriate image URL
     
    Weirfire, Mar 27, 2008 IP
  4. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I am paying $7 for the code!

    P.S: For the other script what must I do, I downloaded it before but it was too hard for me to understand.

    + I want it to add the description.

    Thanks
     
    AT-XE, Mar 27, 2008 IP
  5. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #5
    
    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;
            $tnl = $item->getElementsByTagName("image");
            $tnl = $tnl->item(0);
            $image = $tnl->firstChild->data;
            $y["title"] = $title;
            $y["link"] = $link;
            $y["description"] = $description;
            $y["type"] = $type;
            $y["image"] = $image;
            return $y;
    }
    
    PHP:
    Try that for the first function then use $article["image"] in the last function where you are creating your "$page".

    Magpie RSS isn't too hard to install. ;)
     
    Weirfire, Mar 27, 2008 IP
  6. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ok, PM me your paypal and tell me what to put more, I added this code.
    Now what, I don't have an idea of php.
     
    AT-XE, Mar 27, 2008 IP
  7. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #7
    never worry about the $7. :)
     
    Weirfire, Mar 27, 2008 IP
    AT-XE likes this.
  8. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #8
    so you don't want money?

    Thank you very much!

    +rep added
     
    AT-XE, Mar 27, 2008 IP
  9. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #9
    Nah, it's alright. I'll just call in a favour sometime in the future :)
     
    Weirfire, Mar 27, 2008 IP
  10. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #10
    :)

    Thank you ;)

    But where must the rest of the code be?

    Thanks again.
     
    AT-XE, Mar 27, 2008 IP
  11. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #11
    Do you have a URL for your site?
     
    Weirfire, Mar 27, 2008 IP
  12. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #12
    no it is in my localhost. But I will upload it if you want.
     
    AT-XE, Mar 27, 2008 IP
  13. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #13
    That file now looks:

    <?php
    $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;
            $tnl = $item->getElementsByTagName("image");
            $tnl = $tnl->item(0);
            $image = $tnl->firstChild->data;
            $y["title"] = $title;
            $y["link"] = $link;
            $y["description"] = $description;
            $y["type"] = $type;
            $y["image"] = $image;
            return $y;
    }
    
    
    function RSS_Channel($channel)
    {
    	global $RSS_Content;
    
    	$items = $channel->getElementsByTagName("item");
    	
    	foreach($items as $item)
    	{
    		$y = RSS_Tags($item, 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 = "";
    
    	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"];
    		$desc = $article["description"];
    		$link = $article["link"];
    		$page .= "<li><a href=\"$link\">$title</a></li>$desc\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"];
    		$article["link"] = false;
    		$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";
    	
    }
    
    
    ?>
    PHP:
     
    AT-XE, Mar 27, 2008 IP
  14. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #14
    There's not really much else to say...

    on the RSS_Display function put $article["image"] whereever you want the image to appear.

    As long as the URL you are using to pull in all the values has an id assigned to each image as "image" then it should work when you call the RSS_Display function.
     
    Weirfire, Mar 27, 2008 IP
  15. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #15
    I don't want images to appear. But don't show them...
     
    AT-XE, Mar 27, 2008 IP
  16. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #16
    ??? What do you mean?
     
    Weirfire, Mar 27, 2008 IP
  17. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #17
    I want to remove Images, they are already showing.
    I don't want them to be shown.

    Thanks.
     
    AT-XE, Mar 27, 2008 IP
  18. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #18
    Ahh ok, so they must be a part of the description text.

    You want to put this in the RSS_Tags function right after the $y["description"] = .... line of code;
    
    $y["description"] = preg_replace('#(<[/]?img.*>)#U', '', $y["description"]); 
    PHP:
     
    Weirfire, Mar 27, 2008 IP
  19. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #19
    thank you!!!!!!!!!
     
    AT-XE, Mar 27, 2008 IP
  20. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #20
    All working then? :)
     
    Weirfire, Mar 27, 2008 IP