删除以“;”开头的行

delete lines start with " ;"

我在 PHP 中有一个字符串:

string(765) " ; <<>> DiG 9.9.5-9+deb8u10-Debian <<>> -t a webtools.hu @217.65.97.38 ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62425 ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 3 ;; WARNING: recursion requested but not available ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;webtools.hu.  IN  A ;; ANSWER SECTION: webtools.hu.   60  IN  A 217.65.97.116 ;; AUTHORITY SECTION: webtools.hu.  60  IN  NS  ns2.wwdh.hu. webtools.hu.   60  IN  NS  ns1.wwdh.hu. ;; ADDITIONAL SECTION: ns1.wwdh.hu.    60  IN  A   213.239.206.117 ns2.wwdh.hu.    60  IN  A   217.65.97.38 ;; Query time: 1 msec ;; SERVER: 217.65.97.38#53(217.65.97.38) ;; WHEN: Tue Apr 04 17:25:11 CEST 2017 ;; MSG SIZE rcvd: 129 "

我想删除所有以 space 和分号 (;)

开头的行

我尝试使用 preg_replace 但它没有给出正确答案。

$eredmeny1 = preg_replace('/(^ ;)+/', '', $output);

你能告诉我吗?

$string = " ; <<>> DiG 9.9.5-9+deb8u10-Debian 
vds
jfgh gdf
 ; <<>> DiG 9.9.5-9+deb8u10-Debian
hgf";
$eredmeny1 = preg_replace("/(?:[\r\n]+|^) ;.*\n/", "", $output);

// Output: vds
//         jfgh gdfhgf";

如果您想从文件中读取并删除所有字符串,请尝试以下代码:

<?
$sample = ' ;';
$arr = file('text.txt');
$handle = fopen('text.txt','w+');
foreach($arr as $string){
  if(strpos($string,$sample)===false) fwrite($handle,$string);
}
fclose($handle);

您的问题需要更清楚。我不确定您不想 "explode" 字符串并从给定数组中收集文本。

<?php

$output = " ; <<>> DiG 9.9.5-9+deb8u10-Debian <<>> -t a webtools.hu 
@217.65.97.38 ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- 
opcode: QUERY, status: NOERROR, id: 62425 ;; flags: qr aa rd; QUERY: 1, 
ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 3 ;; WARNING: recursion requested 
but not available ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 
4096 ;; QUESTION SECTION: ;webtools.hu.  IN  A ;; ANSWER SECTION: 
webtools.hu.   60  IN  A 217.65.97.116 ;; AUTHORITY SECTION: webtools.hu.  
60  IN  NS  ns2.wwdh.hu. webtools.hu.   60  IN  NS  ns1.wwdh.hu. ;; 
ADDITIONAL SECTION: ns1.wwdh.hu.    60  IN  A   213.239.206.117 
ns2.wwdh.hu.    60  IN  A   217.65.97.38 ;; Query time: 1 msec ;; SERVER: 
217.65.97.38#53(217.65.97.38) ;; WHEN: Tue Apr 04 17:25:11 CEST 2017 ;; 
MSG SIZE rcvd: 129 ";

$tset = explode(" ;",$output);
foreach($tset as $t){ echo"$t \n";}
?>

...也许您可以 "exec()" "sed -ie 's/^ ;//g' " 使用正确的 POSIX 正则表达式。