Reputation: 21
well i know that how to replace string using PHP str_replace. I have code for multiple string replace it works fine
$subject = 'afsdfasdfasdfasd #%^#^%#@@';
$string = array('a','b','c','d','e','@','#','%','!');
echo str_replace($string, '', $subject);
i have put many words on text file one by one like
01
02
03
04
05
06
07
08
09
http://
stackoverflow.com
questions
ask
gfx
value
words
that
i want
not
like
to
appear
in
title
and named it 'replace.txt' now my question is how i can load this text file for string replace function replace with empty
Upvotes: 0
Views: 154
Reputation: 101
Try this
$text = file_get_contents("replace.txt");
$terms = explode("\n", $text);
$string = "Hello World";
$string = str_replace($terms, '', $string);
echo($string);
I edited this so replace.txt is the terms that are removed
Upvotes: 1
Reputation: 302
Although the question isn't clearly stated, I suppose it's essentially this: You have a file with text (i.e. words separated by spaces) and you want to delete all words that occur in the list.
Assume $text
contains the text to process, and $terms
the lines like you posted above. Turn both into an array:
$text = explode(' ', $text);
$terms = explode("\n", $terms);
$text = array_diff($text, $terms);
$text = implode(' ', $text);
Of course you may need extra processing to get rid of punctuation and other stuff, but array_diff
is essentially what does the job.
Upvotes: 1