3
$file = file_get_contents("http://www.bigsite.com");

How could i go about removing all lines from string $file that contains the word "hello" ?

4

3 回答 3

12
$file = file_get_contents("http://www.bigsite.com");
$lines = explode("\n", $file);
$exclude = array();
foreach ($lines as $line) {
    if (strpos($line, 'hello') !== FALSE) {
         continue;
    }
    $exclude[] = $line;
}
echo implode("\n", $exclude);
于 2013-03-02T02:10:40.367 回答
2
$file = file_get_contents("http://www.example.com");

// remove sigle word hello
echo preg_replace('/(hello)/im', '', $file);

// remove multiple words hello, foo, bar, foobar
echo preg_replace('/(hello|foo|bar|foobar)/im', '', $file);

编辑删除线

// read each file lines in array
$lines = file('http://example.com/');

// match single word hello
$pattern = '/(hello)/im';

// match multiple words hello, foo, bar, foobar
$pattern = '/(hello|foo|bar|foobar)/im';

$rows = array();

foreach ($lines as $key => $value) {
    if (!preg_match($pattern, $value)) {
        // lines not containing hello
        $rows[] = $line;
    }
}

// now create the paragraph again
echo implode("\n", $rows);
于 2013-03-02T02:09:05.893 回答
1

干得好:

$file = file('http://www.bigsite.com');

foreach( $file as $key=>$line ) {
  if( false !== strpos($line, 'hello') ) {
    unset $file[$key];
  }
}

$file = implode("\n", $file);
于 2013-03-02T02:18:52.523 回答