2017-04-04 82 views
0

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

回答

0
$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

謝謝,正則表達式與爆炸函數一起工作。再次感謝您 –

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); 
0

你的問題需要更清晰。我不確定你不想「爆炸」字符串,並從給定數組中提取文本。

<?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

謝謝,爆炸是個好主意! –