I am new to php and especially for regex. My goal is to automatically enrich texts with hints for the "keywords" that are listed in arrays.
So far I have come.
$pattern = array("/\bexplanations\b/i",
"/\btarget\b/i",
"/\bhints\b/i",
"/\bhint\b/i",
);
$replacement = array("explanations <i>(Erklärungen)</i>",
"target <i>Ziel</i>",
"hints <i>Hinsweise</i>",
"hint <i>Hinweis</i>",
);
$string = "Target is to add some explanations (hints) from an array to
this text. I am thankful for every hint.";
echo preg_replace($pattern, $replacement, $string);
returns:
target <i>Ziel</i> is to add some explanations <i>(Erklärungen)</i> (hints <i>Hinsweise</i>) from an array to this text. I am thankful for every hint <i>Hinweis</i>
1) In general, I wonder if there are more elegant solutions (ultimately, without replacing the original word)? In a later state, arrays will contain more than 1000 elements ... and come from mariadb.
2) How can I get the word “Goals” to receive case-sensitive treatment? (without duplicating the length of the arrays).
Sorry for my english and thank you very much in advance.
source
share