1. Trang chủ
  2. » Công Nghệ Thông Tin

Plug in PHP 100 POWER SOLUTIONS- P41 ppt

5 110 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 250,53 KB

Nội dung

166 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s 166 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s $description = "This feed was converted from HTML"; $url = "http://myserver.com"; $webmaster = "webmaster@myserver.com"; $copyright = "Copyright 2010 myserver.com"; header('Content-Type: text/xml'); echo PIPHP_HTMLToRSS($html, $title, $description, $url, $webmaster, $copyright); Or, you can convert almost any HTML page on the Web by using code such as this: $url = "http://www.mhprofessional.com/"; $html = file_get_contents($url); $title = "RSS version of '$url'"; $description = "The website '$url' converted to an RSS feed"; $webmaster = "nobody@nowhere.com"; $copyright = "Copyright $url"; header('Content-Type: text/xml'); echo PIPHP_HTMLToRSS($html, $title, $description, $url, $webmaster, $copyright); Remember that because this plug-in relies on plug-in 21, PIPHP_RelToAbsURL(), you must make sure you have also copied it into your program, or otherwise included it. The Plug-in function PIPHP_HTMLToRSS($html, $title, $description, $url, $webmaster, $copyright) { $date = date("D, d M Y H:i:s e"); $html = str_replace('&amp;', '&', $html); $html = str_replace('&', '!!**1**!!', $html); $dom = new domdocument(); @$dom ->loadhtml($html); $xpath = new domxpath($dom); $hrefs = $xpath->evaluate("/html/body//a"); $links = array(); $to = array(); $count = 0; for ($j = 0 ; $j < $hrefs->length ; ++$j) $links[] = $hrefs->item($j)->getAttribute('href'); $links = array_unique($links); sort($links); foreach ($links as $link) { if ($link != "") { $temp = str_replace('!!**1**!!', '&', $link); $to[$count] = urlencode(PIPHP_RelToAbsURL($url, $temp)); C h a p t e r 7 : T h e I n t e r n e t 167 C h a p t e r 7 : T h e I n t e r n e t 167 $html = str_replace("href=\"$link\"", "href=\"!!$count!!\"", $html); $html = str_replace("href='$link'", "href='!!$count!!'", $html); $html = str_replace("href=$link", "href=!!$count!!", $html); ++$count; } } for ($j = 0 ; $j < $count ; ++$j) $html = str_replace("!!$j!!", $to[$j], $html); $html = str_replace('http%3A%2F%2F', 'http://', $html); $html = str_replace('!!**1**!!', '&', $html); $html = preg_replace('/[\s]+/', ' ', $html); $html = preg_replace('/<script[^>]*>.*?<\/script>/i', '', $html); $html = preg_replace('/<style[^>]*>.*?<\/style>/i', '', $html); $ok = '<a><i><b><u><s><h><img><div><span><table><tr>'; $ok .= '<th><tr><td><br><p><ul><ol><li>'; $html = strip_tags($html, $ok); $html = preg_replace('/<h[1-7][^>]*?>/i', '<h>', $html); $html = htmlentities($html); $html = preg_replace("/&lt;h&gt;/si", "</description></item>\n<item><title>", $html); $html = preg_replace("/&lt;\/h[1-7]&gt;/si", "</title><guid>$url</guid><description>", $html); return <<<_END <?xml version="1.0" encoding="UTF-8"?> <rss version="2.0"><channel> <generator>Pluginphp.com: plug-in 48</generator> <title>$title</title><link>$url</link> <description>$description</description> <language>en</language> <webMaster>$webmaster</webMaster> <copyright>$copyright</copyright> <pubDate>$date</pubDate> <lastBuildDate>$date</lastBuildDate> <item><title>$title</title> <guid>$url</guid> <description>$html</description></item></channel></rss> _END; } 168 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s 168 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s RSS to HTML This plug-in provides the inverse functionality of plug-in 48—it converts an RSS feed into standard HTML format. It’s perfect for when you don’t have a feed reader on hand or wish to grab some syndicated content and put it in one of your own web pages (if you do though, make sure you have any required permissions). Figure 7-9 shows the NASA Image of the Day RSS feed after converting it to HTML. You can access this feed at www.nasa.gov/rss/image_of_the_day.rss. About the Plug-in This plug-in accepts a string containing the contents of an RSS feed to be converted and returns that string transformed into HTML. It takes this argument: • $rss The contents of an RSS feed to convert FIGURE 7-9 This plug-in will convert RSS feeds into regular HTML web pages. 49 C h a p t e r 7 : T h e I n t e r n e t 169 C h a p t e r 7 : T h e I n t e r n e t 169 Variables, Arrays, and Functions $xml XML object created from $rss $title String extracted from the RSS title tag $link String extracted from the RSS link tag $desc String extracted from the RSS description tag $copyr String extracted from the RSS copyright tag $ilink String extracted from the RSS image link tag $ititle String extracted from the RSS image title tag $iurl String extracted from the RSS image url tag $out String containing converted HTML $tlink String containing link of current item $tdate String containing publication date of current item $ttitle String containing title of current item $tdesc String containing description of current item How It Works This plug-in uses the simplexml_load_string() function to create an XML object in $xml, from the RSS feed in $rss. From there the string variables $title, $link, $desc, $copyr, $ilink, $ititle, and $iurl are easily assigned by extracting the various items from $xml->channel. In case any items do not exist in the RSS feed, each of these extractions is prefaced by an @ symbol which will suppress any error messages. Next, the string variable $out is initialized with the opening tags for a standard HTML document and then, if an image was specified in the feed, the image and its associated title and description are also added. I have decided to align the image to the left and allow the description to butt up to the right of it because the main image in an RSS feed is generally a logo. This seems to work well with most other main images, too. After that, <h1> and <h2> headings containing the main RSS feed title and description are added to $out. Then a foreach loop is used to iterate through every item within the feed. As each is extracted, its details are placed in the variables $tlink, $tdate, $ttitle, and $tdesc, from where they are appended to $out, within suitable HTML heading and paragraph tags. And that’s it. All that remains is to return $out, along with the copyright string in $copyr and the closing HTML tags. How to Use It To convert an RSS feed to HTML, just pass it to the plug-in, like this: $url = "http://www.nasa.gov/rss/image_of_the_day.rss"; $rss = file_get_contents($url); echo PIPHP_RSSToHTML($rss); As you can see, the feed can be pulled in from anywhere on the Web, or it can be a feed from your own site. All you need to do is pass the feed itself (not the URL it came from) to the plug-in and display the result returned. 170 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s 170 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s Alternatively, to insert the HTML version of the feed into your own web pages, just use the string returned by the plug-in, like this: $result = PIPHP_RSSToHTML($rss); echo "<h1>Here's NASA's image of the day</h1>$result"; The Plug-in function PIPHP_RSSToHTML($rss) { $xml = simplexml_load_string($rss); $title = @$xml->channel->title; $link = @$xml->channel->link; $desc = @$xml->channel->description; $copyr = @$xml->channel->copyright; $ilink = @$xml->channel->image->link; $ititle = @$xml->channel->image->title; $iurl = @$xml->channel->image->url; $out = "<html><head><style> img {border: 1px solid " . "#444444}</style>\n<body>"; if ($ilink != "") $out .= "<a href='$ilink'><img src='$iurl' title=" . "'$ititle' alt='$ititle' border='0' style=" . "'border: 0px' align='left' /></a>\n"; $out .= "<h1>$title</h1>\n<h2>$desc</h2>\n"; foreach($xml->channel->item as $item) { $tlink = @$item->link; $tdate = @$item->pubDate; $ttitle = @$item->title; $tdesc = @$item->description; $out .= "<h3><a href='$tlink' title='$tdate'>" . "$ttitle</a></h3>\n<p>$tdesc</p>\n"; } return "$out<a href='$link'>$copyr</a></body></html>"; } HTML to Mobile The final plug-in in this chapter will take an HTML page and format it in such a way that it will load faster on a mobile browser that may have limited download speeds, and also display better due to removing a lot of style and formatting information. Figure 7-10 shows the www.yahoo.com web page after being processed by this plug-in. As you can see in Figure 7-11, the plug-in has substantially reduced the original web page. 50 . HTML $tlink String containing link of current item $tdate String containing publication date of current item $ttitle String containing title of current item $tdesc String containing description. www.nasa.gov/rss/image_of_the_day.rss. About the Plug- in This plug- in accepts a string containing the contents of an RSS feed to be converted and returns that string transformed into HTML. It takes this argument: •. due to removing a lot of style and formatting information. Figure 7-10 shows the www.yahoo.com web page after being processed by this plug- in. As you can see in Figure 7-11, the plug- in has substantially

Ngày đăng: 07/07/2014, 08:20