Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用正则表达式删除没有字母字符的行?_Php_Regex - Fatal编程技术网

Php 如何使用正则表达式删除没有字母字符的行?

Php 如何使用正则表达式删除没有字母字符的行?,php,regex,Php,Regex,我有一个代码来清除xml文件中的标记和不必要的信息。除了最后一个preg_替换外,一切正常,它不会删除不包含任何字母字符的行。输出包括以下行: 153 834 4598 6 0 代码如下 $xml = file_get_contents('3377035.xml'); $a = strip_tags($xml,""); $a = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $a); $a = preg

我有一个代码来清除xml文件中的标记和不必要的信息。除了最后一个preg_替换外,一切正常,它不会删除不包含任何字母字符的行。输出包括以下行:

  153
  834
  4598
  6
  0
代码如下

$xml = file_get_contents('3377035.xml');

$a =  strip_tags($xml,"");
$a = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $a);
$a = preg_replace("/^([^A-Za-z]+)/", "", $a);

file_put_contents("new.txt", $a);

我找到了答案:我应该添加多行标志:

$a = preg_replace("/^([^A-Za-z]+)/m", "", $a);

要删除不包含字母的行,您需要
'~^[^\p{L}\r\n]+\r*~m'
。如果您有Unicode文本,请在
m
之后添加
u
。@WiktorStribiżew thx,是的,多行标志完成了任务,thx)而不是“清除不必要的信息”,使用XML解析器提取您想要的信息。