Найдите ключевые слова в тексте и добавьте гиперссылки на ключевые слова.
Найдите ключевые слова в тексте, добавьте гиперссылку на ключевые слова, если есть необходимость в замене ключевых слов, ее все равно можно изменить на основе этого класса. Порядок подстановки зависит от индекса массива. Правила могут быть записаны в данные, и поля веса могут быть добавлены. Приоритет замены ключевых слов или гиперссылок может быть динамически скорректирован.
php /** * Add a connection to the matching characters in the article * Created by PhpStorm. * User: smallForest<[email protected]> * Date: 2019-06-06 * Time: 09:19 */ class addLink { protected $content = ''; protected $replace_rules = []; public function __construct($content, $replace_rules) { $this->content = $content; $this->replace_rules = $replace_rules; } public function do_replace() { // Perform substitution to return the substituted string if (!empty($this->replace_rules)) { foreach ($this->replace_rules as $rule) { $this->content = preg_replace('/(?!<[^>]*)' . $rule['key_word'] . '(?![^<]*(>|<\/[a|sc]))/s', '' . $rule['key_word'] . "", $this->content, $rule['replace_times'], $count; // You can see the replacement result by judging that the count field is greater than 0 } } return $this->content; } } $rule = [ [ 'key_word'=>'Chinese', //keywords 'url'=>'http://www.baidu.com?Id=Chinese', //Need to add hyperchains 'target'=>'_blank', //Open mode 'replace_times'=> 1, // / the number of times allowed to replace - 1 is an unrestricted number of times! ], [ 'key_word'=>'China', //keywords 'url'=>'http://www.baidu.com?Id=China', //Need to add hyperchains 'target'=>'_blank', //Open mode 'replace_times'=> 1, // / the number of times allowed to replace - 1 is an unrestricted number of times! ], [ 'key_word'=>'person', 'url'=>'http://www.baidu.com?Id=human', 'target' => '_blank', 'replace_times' => 1, ], ]; $obj = new addLink ('I'm Chinese', $rule); echo $obj->do_replace();