Replacing words with tag links in PHP

I have text ( $text ) and an array of words ( $tags ). These words in the text should be replaced by links to other pages so that they do not violate existing links in the text. CakePHP has a TextHelper method for this, but it is corrupted and breaks existing HTML links in the text. The method should work as follows:

 $text=Text->highlight($text,$tags,'<a href="/tags/\1">\1</a>',1); 

The CakePHP TextHelper code has existing code below:

 function highlight($text, $phrase, $highlighter = '<span class="highlight">\1</span>', $considerHtml = false) { if (empty($phrase)) { return $text; } if (is_array($phrase)) { $replace = array(); $with = array(); foreach ($phrase as $key => $value) { $key = $value; $value = $highlighter; $key = '(' . $key . ')'; if ($considerHtml) { $key = '(?![^<]+>)' . $key . '(?![^<]+>)'; } $replace[] = '|' . $key . '|ix'; $with[] = empty($value) ? $highlighter : $value; } return preg_replace($replace, $with, $text); } else { $phrase = '(' . $phrase . ')'; if ($considerHtml) { $phrase = '(?![^<]+>)' . $phrase . '(?![^<]+>)'; } return preg_replace('|'.$phrase.'|i', $highlighter, $text); } } 
+6
php regex tags cakephp
source share
4 answers

Here you can see (and run) this algorithm:

http://www.exorithm.com/algorithm/view/highlight

This can be done a little better and easier with a few changes, but it's still not perfect. Although less efficient, I would recommend one of Ben Doom's solutions.

+2
source share

Replacing text in HTML is fundamentally different from replacing plain text. To determine if the text is part of an HTML tag, you need to find all the tags so that they are not taken into account. Regex is not really a tool for this.

I would try to execute one of the following solutions:

  • Find the positions of all words. As you work from last to first, determine if each is part of a tag. If not, add an anchor.
  • Divide the line into blocks. Each block is either a tag or plain text. Run your replacement (s) on the text blocks and reassemble.

I think the first one is probably a bit more efficient, but more prone to programmer errors, so I will leave it to you.

If you want to know why I donโ€™t approach this problem directly, look at all the questions on the site about regular expressions and HTML and how regular expressions are not a parser.

+1
source share

This code works fine. What you might need is to check the CSS for <span class="highlight"> and make sure that it is set to some color that allows you to distinguish it from high light.

 .highlight { background-color: #FFE900; } 
0
source share

Amorphous - I noticed that Geert edited your post. Are two pieces of code exactly as they are placed?

So, although the source code was intended to be allocated, I understand that you are trying to repurpose it to generate links - it should and works fine for it (checked as sent).

NEVER escaping the first piece of code can be a problem.

 $text=Text->highlight($text,$tags,'<a href="/tags/\1">\1</a>',1); 

Works great ... but if you use speach characters rather than quotation marks, backslashes disappear like escape marks - you need to avoid them. If you do not get links% 01.

The correct way with signs:

 $text=Text->highlight($text,$tags,"<a href=\"/tags/\\1\">\\1</a>",1); 

(Note the use of \ 1 instead of \ 1)

0
source share

All Articles