删除行以"开头;"

时间:2017-04-04 15:33:01

标签: php regex preg-replace

我在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);
你可以告诉我吗?

3 个答案:

答案 0 :(得分: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";

答案 1 :(得分:0)

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

k

答案 2 :(得分: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

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