插件22:从URL地址读取链接信息

<?php // Plug-in 22: Get Links From URL

// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

$result = PIPHP_GetLinksFromURL("http://pluginphp.com");

echo "<ul>";
for ($j = 0 ; $j < count($result) ; ++$j)
   echo "<li>$result[$j]</li>";

function PIPHP_GetLinksFromURL($page)
{
   // Plug-in 22: get Links From URL
   //
   // This plug-in accepts the URL or a web page and returns
   // an array of all the links found in it. The argument
   // required is:
   //
   //    $page: The web site's main URL

   $contents = @file_get_contents($page);
   if (!$contents) return NULL;
   
   $urls    = array();
   $dom     = new domdocument();
   @$dom    ->loadhtml($contents);
   $xpath   = new domxpath($dom);
   $hrefs   = $xpath->evaluate("/html/body//a");

   for ($j = 0 ; $j < $hrefs->length ; $j++)
      $urls[$j] = PIPHP_RelToAbsURL($page,
         $hrefs->item($j)->getAttribute('href'));

   return $urls;
}

// The below function is repeated here to ensure that it's
// available to the main function which relies on it

function PIPHP_RelToAbsURL($page, $url)
{
   // Plug-in 21: Relative To Absolute URL
   // This plug-in accepts the absolute URL of a web page
   // and a link featured within that page. The link is then
   // turned into an absolute URL which can be independently
   // accessed. Only applies to http:// URLs. Arguments are:
   //    $page: The web page containing the URL
   //    $url:  The URL to convert to absolute

   if (substr($page, 0, 7) != "http://") return $url;
   
   $parse = parse_url($page);
   $root  = $parse['scheme'] . "://" . $parse['host'];
   $p     = strrpos(substr($page, 7), '/');
   
   if ($p) $base = substr($page, 0, $p + 8);
   else $base = "$page/";
   
   if (substr($url, 0, 1) == '/')           $url = $root . $url;
   elseif (substr($url, 0, 7) != "http://") $url = $base . $url;
   
   return $url;
}
?>

插件说明:

本插件接受一个web页面的URL地址,对他进行解析,只寻找"<a href "超链接标签,以数组的形式返回所有找到的超链接地址。他需要一个参数:

$page: 一个web页面的URL地址,包括前导符“http://”和域名。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用JSP和MySQL实现从数据库中读取音乐文件的步骤如下: 1. 在MySQL数据库中创建一个音乐文件的表,其中包含音乐文件的名称、路径、类型等信息。 2. 在JSP页面中编写代码,连接到MySQL数据库,查询音乐文件的表,获取需要的音乐文件信息。 3. 在JSP页面中使用HTML5的audio标签或者其他音频播放器插件,将获取到的音乐文件信息呈现在页面上,实现音乐的播放。 下面是一个简单的示例代码: 在MySQL数据库中创建一个名为music的表,表中包含id、name、path和type四个字段,其中id为自增主键。 在JSP页面中编写以下代码: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.sql.*" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>音乐播放器</title> </head> <body> <h1>音乐列表</h1> <% // 定义数据库连接信息 String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/test?useSSL=false"; String user = "root"; String password = "password"; // 定义SQL查询语句 String sql = "SELECT id, name, path, type FROM music"; // 声明数据库连接和结果集 Connection conn = null; ResultSet rs = null; try { // 加载数据库驱动程序 Class.forName(driver); // 建立数据库连接 conn = DriverManager.getConnection(url, user, password); // 创建Statement对象,执行SQL查询语句 Statement stmt = conn.createStatement(); rs = stmt.executeQuery(sql); // 遍历结果集,输出音乐文件信息 while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); String path = rs.getString("path"); String type = rs.getString("type"); %> <p><a href="<%=path%>" target="_blank"><%=name%></a> (<%=type%>)</p> <% } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭数据库连接和结果集 try { if (rs != null) { rs.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } %> </body> </html> ``` 该示例代码实现了从MySQL数据库中读取音乐文件信息,并将其以链接的形式展示在页面上。您可以根据您的实际需求,将其改造成一个完整的音乐播放器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值