$str = 'Example: hello world, this world rocks.
What it should do is:
if it finds the word hello it should
remove the whole line. How can i do that and there
could be words in between brackets and inverted commas also.';
$lines = explode("\n", $str);
foreach($lines as $index => $line) {
if (strstr($line, 'hello')) {
unset($lines[$index]);
}
}
$str = implode("\n", $lines);
var_dump($str);
Вывод
string(137) "What it should do is:
remove the whole line. How can i do that and there
could be words in between brackets and inverted commas also."
CodePad .
Вы сказали, что слово может быть может быть словами в скобках и кавычками тоже.
В случае, если слово требуется только само по себе или между скобками и кавычками, вы можете заменить strstr()
на это ...
preg_match('/\b["(]?hello["(]?\b/', $str);
Ideone .
Я предполагал, что в скобках вы подразумевали круглые скобки и кавычки, а в двойных кавычках.
Вы также можете использовать регулярное выражение в многострочном режиме, однако это не будетс первого взгляда очевидно, что делает этот код ...
$str = trim(preg_replace('/^.*\b["(]?hello["(]?\b.*\n?/m', '', $str));
Смежный вопрос .