加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

使用php在html网页上计数单词

发布时间:2020-12-13 16:41:04 所属栏目:PHP教程 来源:网络整理
导读:我需要一个 PHP脚本,它接收一个网页的URL,然后回应一个单词被提及多少次. 例 这是一个通用的HTML页面: htmlbodyh1 This is the title /h1p some description text here,bthis/b is a word. /p/body/html 这将是PHP脚本: ?phphtmlurl="generichtml.com";the
我需要一个 PHP脚本,它接收一个网页的URL,然后回应一个单词被提及多少次.

这是一个通用的HTML页面:

<html>
<body>
<h1> This is the title </h1>
<p> some description text here,<b>this</b> is a word. </p>
</body>
</html>

这将是PHP脚本:

<?php
htmlurl="generichtml.com";
the script here
echo(result);
?>

所以输出将是这样的表:

WORDS       Mentions
This        2
is          2
the         1
title       1
some        1
description 1
text        1
a           1
word        1

这就像搜索机器人在上网时所做的那样,所以,有什么想法,如何开始,甚至更好,你有一个PHP脚本已经这样做了吗?

从您的字符串中删除所有HTML标记后,下面的一行将执行不区分大小写的字数.

Live Example

print_r(array_count_values(str_word_count(strip_tags(strtolower($str)),1)));

要获取页面的源代码,可以使用cURLfile_get_contents()

$str = file_get_contents('http://www.example.com/');

从内到外:

>使用strtolower()使所有小写.
>使用strip_tags()剥离HTML标签
>创建使用str_word_count()使用的单词数组.参数1返回一个数组,其中包含字符串内的所有单词.
>使用array_count_values()通过计算单词数组中每个值的出现来捕获多次使用的单词.
>使用print_r()显示结果.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读