如果单词存在,如何删除行? (PHP) [英] How to Remove line if word exists? (PHP)

查看:82
本文介绍了如果单词存在,如何删除行? (PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,如果要删除整行,我想删除整行吗?通过PHP?

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

示例:hello world, this world rocks. 它应该做的是:如果找到单词hello,则应删除整行. 我该怎么做,方括号和逗号之间也可能有单词.

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 = 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/zh/function.stripos.php

这篇关于如果单词存在,如何删除行? (PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆