自动标记HTML文本

时间:2015-03-28 18:40:53

标签: php jquery html css string

我希望找到一种基于文本匹配自动标记HTML文档的方法。我有一个包含数百个简短HTML文档的目录,其内容在base_document.html中单独包含(通过PHP),具体取决于从URL获取的变量的值,如下所示:

<?php include $_GET['id'] . ".html";?>

如果包含的HTML文档包含特定字词,例如“危险”,我想用特定的标记自动格式化它;例如:

<strong>dangerous</strong>

PHP,JavaScript和/或CSS中是否已存在提供此类功能的特定库或函数?或者我需要编写自己的代码来实现我的目的吗?

编辑:会有一长串目标词和相关标记,所以我假设我需要引用一个驻留在文件或数据库中的自定义词典。如,

"dangerous",bold
"important",style:color=red
"hungry",em
"hot",strong

1 个答案:

答案 0 :(得分:0)

这只是一个例子,你必须适应你的情况:

<?php

$data[] = 'dangerous,bold';
$data[] = 'important,style:color=red';
$data[] = 'hungry,em';
$data[] = 'hot,strong';

$string = 'They are dangerous and hungry but I think that is hot talk about this.';

for($i = 0; $i < count($data); $i++) {
  $line = explode(',', $data[$i]);
  $string = str_replace($line[0], "<{$line[1]}>{$line[0]}</{$line[1]}>", $string);
}   

echo $string;
?>

输出:

They are <bold>dangerous</bold> and <em>hungry</em> but I think that is <strong>hot</strong> talk about this.
相关问题