I want to match an array against a text string where words matching with the array keys will be replaced by a span tag with a title with the matching array value.
I can do that with str_ireplace but then the matched words lose their case:
$glossary = array(
'word1' => '<span title="Explanation for Word 1">word1</span>',
'word2' => '<span title="Explanation for Word 2">word2</span>',
'word3' => '<span title="Explanation for Word 3">word3</span>'
);
$text = "This is a text about Word1. We also talk about word2 and WORD3. Also woRd3, word2 and WORD1";
list($keys, $values) = array_divide($glossary);
$result = str_ireplace($keys, $values, $text);
As said, this works but in this solution all matching words will be in lowercase.
What I want to achieve is this:
'This is a text about <span title="Explanation for Word 1">Word1</span>.
We also talk about <span title="Explanation for Word 2">word2</span> and
<span title="Explanation for Word 3">WORD3</span>.
Also <span title="Explanation for Word 3">woRd3</span>,
<span title="Explanation for Word 2">word2</span>
and <span title="Explanation for Word 1">WORD1</span>'
Do the replacements and keep the case intact.
I found a great reply that came close but it only works with the addition of the span tag around the matched words, it does not add a second variable for the title (php str_ireplace without losing case).
I don't know enough regex (It dazzles me) to change it by adding a second variable for the span titles:
$txt = 'Hi! How are you doing? Have some stars: * * *!';
$array_of_words = array('Hi!', 'stars', '*');
$pattern = '#(?<=^|\W)('
. implode('|', array_map('preg_quote', $array_of_words))
. ')(?=$|\W)#i';
echo preg_replace($pattern,
"<span style='background-color:#EEEE00'>$1</span>", $txt);
You can achieve it with some common PHP functions.
Create an array of patterns from your
keys
to search in a case-insensitive manner withpreg_replace_callback
.Pass the
$glossary
to the anonymous function inpreg_replace_callback
and get the value (the<span>
title) by the lowercased current match value, and once found, replace the lowercasewordX
with the match.This is PHP code:
You can run this replace in loop: