如果单词存在,如何删除行? (PHP)

发布于 2024-10-25 06:21:44 字数 146 浏览 5 评论 0原文

嘿,如果其中存在单词,我想删除整行吗?通过PHP?

示例:你好世界,这个世界很震撼。 它应该做的是:如果它找到单词 hello 它应该删除整行。 我该怎么做,括号和引号之间也可能有单词。

谢谢。

Hey, I want to remove the whole line if a word exists in it? through PHP?

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.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你不是我要的菜∠ 2024-11-01 06:21:46
$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));

相关问题。

$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);

Output

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.

You said the word could be could be words in between brackets and inverted commas also too.

In the case of wanting the word only on its own, or between bracket and quotes, you could replace the strstr() with this...

preg_match('/\b["(]?hello["(]?\b/', $str);

Ideone.

I assumed by brackets you meant parenthesis and inverted commas you meant double quotes.

You could also use a regex in multiline mode, however it won't be as obvious at first glance what this code does...

$str = trim(preg_replace('/^.*\b["(]?hello["(]?\b.*\n?/m', '', $str));

Related Question.

岁吢 2024-11-01 06:21:46

如果您有一个像这样的行数组,

$lines = array(
  'hello world, this world rocks',
  'or possibly not',
  'depending on your viewpoint'
);

您可以循环遍历该数组并查找单词

$keyword = 'hello';
foreach ($lines as &$line) {
  if (stripos($line, $keyword) !== false) {
    //string exists
    $line = '';
  } 
}

int stripos ( string $haystack , string $needle [, int $offset = 0 ] )http://www.php.net/manual/en/function.stripos.php

If you have an array of lines like so

$lines = array(
  'hello world, this world rocks',
  'or possibly not',
  'depending on your viewpoint'
);

You can loop through the array and look for the word

$keyword = 'hello';
foreach ($lines as &$line) {
  if (stripos($line, $keyword) !== false) {
    //string exists
    $line = '';
  } 
}

int stripos ( string $haystack , string $needle [, int $offset = 0 ] ) : http://www.php.net/manual/en/function.stripos.php

迷荒 2024-11-01 06:21:46

又好又简单:

$string = "hello world, this world rocks"; //our string
if(strpos($string, "hello") !== FALSE) //if the word exists (we check for false in case word is at position 0)
{
  $string = ''; //empty the string.
}

Nice and simple:

$string = "hello world, this world rocks"; //our string
if(strpos($string, "hello") !== FALSE) //if the word exists (we check for false in case word is at position 0)
{
  $string = ''; //empty the string.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文