0

I have an array of strings

$array = array("are", "finite", "get", "er");

and a string

$string = "You are definitely getting better today";

I expect this output

You today

I want to remove all the array values from the string efficiently, if array value is part of a word in the string whole word must also be removed from string.

Here is my code on which I am working at the moment

foreach ($array as $a)
{
    $string = str_replace($a, "", $string);
}
echo $string;

It removes array values from the string, but it do not remove words that contain array values.

Edit:

As my strings are user provided and are about 4096 characters long, someone please guide me which answer is more efficient with respect of speed and memory usage.

2 Answers 2

2

Try this (regex approach):

$patterns = ["are", "finite", "get", "er"];
$string = "You are definitely getting better today";

$re = '\w*('.implode('|', $patterns).')\w*';
$string = preg_replace('#'.$re.'#', '', $string);
$string = preg_replace('#\h{2,}#', ' ', $string);
echo $string;
Sign up to request clarification or add additional context in comments.

8 Comments

Is it more efficient with respect of speed and memory usage
As Gordon said you need to benchmark solutions yourself in your server config / context, because optimization depends on many relative factors. In your place I would construct typical-length string and typical-search-pattern list and pass modified versions of solutions with for loop for let's say 1000 retries through a Linux time command and see the results.
What changes are need in your provided code if $patterns are coma separated list e.g. are,finite,get,er
coma separated words in a string or what ? - give exact example. (I already gave you snippet with word list - [] is syntactic sugar for array() which I prefer mostly)
I mean if I provide $patterns ="are,finite,get,er"; instead of $patterns = ["are", "finite", "get", "er"];
|
1

Here is a version with strtok

$patterns = ["are", "finite", "get", "er"];
$string = "You are definitely getting better today";

$tok = strtok($string, ' ');
while ($tok !== false) {
    foreach ($patterns as $pattern) {
        if (strpos($tok, $pattern) !== FALSE) {
            $string = str_replace("$tok ", '', $string);
        }
    }
    $tok = strtok(' ');
}
echo $string;

And here is another regex version:

$string = "You are definitely getting better today";
$patterns = ["are", "finite", "get", "er"];

$regex = sprintf(
    "#\w*(%s)\w*\h*#",
    implode('|', array_map('preg_quote', $patterns))
);
echo preg_replace($regex, '', $string);

The pattern is basically the same as the other one on this page, but it doesn't make removal of whitespace an extra step, but does it in a single pattern instead:

Regex pattern \w*(are|finite|get|er)\w*\h*

Regarding your comment:

please modify your 2nd code so that if word https://www.get.co is in string whole word https://www.get.co must be removed

A word (\w) always matches the ASCII characters [A-Za-z0-9_]. Consequently, it will not match forward slashes, dots or colons. You will need to extend the character class as follows:

"#[\w/:.]*(%s)[\w/:.]*\h*#"

As per performance: benchmark it against some of your input.

5 Comments

As my strings are user provided and are about 4096 characters long, can you please guide me which answer is more efficient with respect of speed and memory usage
Do you mean your 2nd version is more efficient?
@DMP Please benchmark/profile in your scenario to see which suits your app context best.
Thank you @Gordon, I am not good in making regex please modify your 2nd code so that if word https://www.get.co is in string whole word https://www.get.co must be removed, currently it removes only get and remaining https://www..co is left in string, Regards
Thank you very much @Gordon for your time, I think \S should also work.

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.