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

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

EDIT 行の削除

// 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 に答える