Home >Backend Development >PHP Problem >How to implement synonym replacement in php

How to implement synonym replacement in php

藏色散人
藏色散人Original
2020-09-04 09:18:263704browse

How to implement synonym replacement in php: first create a PHP sample file; then define a "strtr_words" method; then obtain the vocabulary library through the "file_get_contents" function; finally implement synonym replacement through "explode" and other function methods. Can.

How to implement synonym replacement in php

Recommended: "PHP Video Tutorial"

php implements SEO pseudo-original synonym replacement function

Recently, I discussed with a friend the SEO pseudo-original issue of PHP regarding synonym replacement. I wrote the following function and made a plug-in for emlog.

function strtr_words($str)
{
$words=array();
$content = file_get_contents(‘words.txt’);//词库
$content = str_replace( “\r”, “”,$content); //去掉换行符(以便兼容Linux主机)
$content = preg_split(‘/\n/’, $content, -1, PREG_SPLIT_NO_EMPTY);//\n分割字符
foreach($content as $k=>$v)
{
if($k!=0)
{
$str_data = explode(‘→’,$v);//关键词分割符
$words+=array(“$str_data[0]“=>”$str_data[1]“);
}
}
return strtr($str,$words);//返回结果
}

The vocabulary words.txt format is as follows:

恳求→哀求
悲悼→哀伤
悲痛→哀思
悲伤→哀痛
顺序→挨次
受饿→挨饿
靠拢→挨近

Note that each line contains a set of synonyms, separated by "→".

The above is the detailed content of How to implement synonym replacement in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn