I'm new to PHP & Arrays even though I read a lot of tutorials already. Please be understanding. :)
My intentions: For a fulltext search I'm trying to replace words within a string with custom other words contained in a list of words. It occurs to me that in order to get whole words rather than the found string as part of another word I have to use preg_replace() with a specific pattern instead of str_replace().
My problem: If a certain word is part of the list of words i.e. [apples], I'm trying to replace i.e. [Short text in a fulltext search] with i.e. [Short apples in a fulltext search]. Naturally the final string looks like [Short apples in a fullapples search] or I get some delimiter warnings.
So I'm trying to figure out a pattern for the preg_replace() function to only replace full words and not strings within combines words. And on top of that I'm pulling the word pairs out of an associative array to have a better overview of the list. This is where I'm a novice.
My trials:
$replacements = array(
'text' => 'apples',
etc…
);
$pattern = '#\b'($searchterm)'\b#;
$searchterm = preg_replace(array_keys($replacements), $pattern, $searchterm);
echo $searchterm;
Thank you for your help!
preg_replace: php.net/manual/en/function.preg-replace.php$searchterm?strtrmay be enough for this task. In order to replace full words and not strings within combines words, we simply can add blank to the head an to the end of the replace pair. Hope my answer will be helpful. Any other question, feel free to comment here.