删除行以“;”开头

问题描述:

我在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 " 

我想删除所有行以空间和分号(;)

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

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

你能告诉我吗?

+1

他们是行吗?或者一个单一的长字符串 – RiggsFolly

+0

我们看到的是以';'开头的单行,所以应用这种正则表达式将不会返回任何内容。 – Dimi

+2

什么是预期产出? – anubhava

$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"; 
+3

考虑为您的代码提供解释 – arghtype

+0

谢谢,正则表达式与爆炸函数一起工作。再次感谢您 –

,如果你想从文件中读取和删除所有字符串,试试这个代码:

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

你的问题需要更清晰。我不确定你不想“爆炸”字符串,并从给定数组中提取文本。

<?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";} 
?> 

OR

...也许你可以 “执行exec()”, “SED - 即 'S/^; // G'” 用正确的POSIX正则表达式。

+0

谢谢,爆炸是个好主意! –