php相关收藏链接,php相关收藏链接是什么

发布时间:2023-01-08

本文目录一览:

  1. 用PHP获取链接及图片路径的方法
  2. 请问php文件中如何添加链接?
  3. 请问PHP程序网站收藏网站链接是怎么弄的

用PHP获取链接及图片路径的方法

<?php
$str = "This is a test.This is a test.This is a test.a href=;img src= //aThis is a test.This is a test.\n" .
       "This is a test.This is a test.a href=;img src= //aThis is a test.This is a test.This is a test.\n" .
       "a href=;img src= //a";
$regex = '/a\s+href=(.*)\s*img\s+src=(.*)\s*\/\/a/';
$output = array();
if (preg_match_all($regex, $str, $matches) !== false) {
    if (isset($matches[1]) && isset($matches[2])) {
        $links = $matches[1];
        $imgs = $matches[2];
        foreach ($links as $key => $link) {
            $img = isset($imgs[$key]) ? $imgs[$key] : '';
            $output[] = "a href=\"{$link}\"img src=\"{$img}\" //a";
        }
    }
}
var_dump($output);

请问php文件中如何添加链接?

PHP添加链接和在HTML中添加链接一致,但需要转换为PHP方式。示例如下:

<?php
// PHP添加链接示例
echo "<a href='zhidao.baidu.com'>百度知道</a>";
?>

请问PHP程序网站收藏网站链接是怎么弄的

一、设计搜索表单

在网站的根目录下建一个 search.htm 文件,内容如下:

<html>
<head>
    <title>搜索表单</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<form name="form1" method="post" action="search.php">
    <table width="100%" cellspacing="0" cellpadding="0">
        <tr>
            <td width="36%">
                <div align="center">
                    <input type="text" name="keyword">
                </div>
            </td>
            <td width="64%">
                <input type="submit" name="Submit" value="搜索">
            </td>
        </tr>
    </table>
</form>
</body>
</html>

二、搜索程序

在根目录下创建 search.php 文件,用来处理 search.htm 表单传过来的数据,内容如下:

<?php
// 获取搜索关键字
$keyword = trim($_POST["keyword"]);
// 检查是否为空
if ($keyword == "") {
    echo "您要搜索的关键字不能为空";
    exit; // 结束程序
}
?>

我们可以用递归的方法遍历所有文件,可以用函数 opendirreaddir,也可以用 PHP Directory 的类。这里使用前者。

<?php
// 遍历所有文件的函数
function listFiles($dir, $keyword, &$array) {
    $handle = opendir($dir);
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            if (is_dir("$dir/$file")) {
                listFiles("$dir/$file", $keyword, $array);
            } else {
                $data = fread(fopen("$dir/$file", "r"), filesize("$dir/$file"));
                if (eregi("<body([^]+)*>(.+)</body>", $data, $b)) {
                    $body = strip_tags($b[2]);
                } else {
                    $body = strip_tags($data);
                }
                if ($file != "search.php") {
                    if (eregi("$keyword", $body)) {
                        if (eregi("<title>(.+)</title>", $data, $m)) {
                            $title = $m[1];
                        } else {
                            $title = "没有标题";
                        }
                        $array[] = "$dir/$file $title";
                    }
                }
            }
        }
    }
    closedir($handle);
}
$array = array();
listFiles(".", $keyword, $array);
foreach ($array as $value) {
    list($filedir, $title) = split("[ ]", $value, 2);
    echo "<a href=\"$filedir\" target=\"_blank\">$title</a><br />";
}
?>

三、功能扩展

  1. 列出内容的标题
    修改匹配逻辑,提取 <title> 标签内容作为标题。
  2. 只搜索网页内容的主题部分
    使用正则表达式提取 <body> 标签内容,并使用 strip_tags 去除 HTML 标签。
  3. 标题上加链接
    将搜索结果以超链接形式展示。
  4. 防止超时
    在文件开头添加 set_time_limit("600"); 防止执行超时。

完整代码

<?php
set_time_limit("600");
// 获取搜索关键字
$keyword = trim($_POST["keyword"]);
// 检查是否为空
if ($keyword == "") {
    echo "您要搜索的关键字不能为空";
    exit; // 结束程序
}
function listFiles($dir, $keyword, &$array) {
    $handle = opendir($dir);
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            if (is_dir("$dir/$file")) {
                listFiles("$dir/$file", $keyword, $array);
            } else {
                $data = fread(fopen("$dir/$file", "r"), filesize("$dir/$file"));
                if (eregi("<body([^]+)*>(.+)</body>", $data, $b)) {
                    $body = strip_tags($b[2]);
                } else {
                    $body = strip_tags($data);
                }
                if ($file != "search.php") {
                    if (eregi("$keyword", $body)) {
                        if (eregi("<title>(.+)</title>", $data, $m)) {
                            $title = $m[1];
                        } else {
                            $title = "没有标题";
                        }
                        $array[] = "$dir/$file $title";
                    }
                }
            }
        }
    }
    closedir($handle);
}
$array = array();
listFiles(".", $keyword, $array);
foreach ($array as $value) {
    list($filedir, $title) = split("[ ]", $value, 2);
    echo "<a href=\"$filedir\" target=\"_blank\">$title</a><br />";
}
?>

至此,你已经完成了一个简单的搜索引擎,可以通过修改内容处理部分来改进它,实现搜索标题或内容的功能,也可以考虑添加分页功能。