Reputation: 605
I could use some help. How can I delete words in a string within variable? For Example:
$var = "test1 test2 ui"; $string="fru test1 frhu test2 vrui ui fehugr";
The output should be:
fru fru vrui fehugr
Thanks in advance for your help!
Upvotes: 0
Views: 111
Reputation: 8459
@xdazz, unfortunately, your code does not return the requested output.
Since Filippo only wants to replace /real/ words, that starts with nothing/whitespace and ends with whitespace/new-line/nothing, you need a regular expression for it.
$exp = '/(^|\s)('.str_replace(' ', '|', preg_quote($var, '/')).')(?=(\s|$))/';
$string = trim(preg_replace($exp, '', $string));
This replaces only real words.
Upvotes: 2
Reputation: 160853
Use the str_replace function:
$ret = str_replace(explode(' ', $var), '', $string);
Upvotes: 3