1

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!

10
  • You should check the manual on preg_replace: php.net/manual/en/function.preg-replace.php Commented Jul 23, 2013 at 1:39
  • Can you give an example of your $searchterm ? Commented Jul 23, 2013 at 1:39
  • I read the manual but I still can't get my head around it. @srain: the $searchterm variable just contains a simple string. Probably never more than three different words. Commented Jul 23, 2013 at 1:46
  • 1
    strtr may 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. Commented Jul 23, 2013 at 1:56
  • Hey srain, thank you very much for your help and explanation! Seems logic to me now. However I still have problems figuring out how to apply [$from] & [$to] if [$searchterm] is just one single word. I guess because it's looking for a space before the word. Should I solve this with a blunt [if] statement or might there be a more elegant solution? Commented Jul 23, 2013 at 2:16

1 Answer 1

1

strtr may 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.

<?php
$replacements = array(
    'text' => 'apples',
);

$realRreplacements = array();
foreach ($replacements as $from => $to)
{
    $from = ' ' . $from . ' ';
    $to = ' ' . $to . ' ';
    $realRreplacements[$from] = $to;
}

$str = 'Short text in a fulltext search';
$str = strtr($str, $realRreplacements);
echo $str, "\n";
// output: Short apples in a fulltext search

-

                      ************ update *************

turn back to preg_replace:

<?php
$list = array(
    'text' => 'apples',
    'Short' => 'Long',
    'search' => 'find',
);
$pattens = array();
$replacement = array();
foreach ($list as $from => $to)
{
    $from = '/\b' . $from . '\b/';
    $pattens[] = $from;
    $replacement[] = $to;
}
$str = 'Short text in a fulltext search';
$str = preg_replace($pattens, $replacement, $str);
echo $str, "\n"; // Long apples in a fulltext find
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much srain! After lunch and some tweaks to match the rest of my code, I managed to get it to work like a charm! Definitely taught me something. And thanks for being so friendly. :)
I reviewed my comment, I meant do not mind that some one gives your a downvote, missed a not. ^_^.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.