Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.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,解析文本(配置文件)的最佳方法是什么,其中设置变量的“样式”不同: var_name = "Content" // comment var_name2 = Content // comment set var_name3 "Content" // comment Var_name4=Content // comment 我只需要变量名和内容,忽略“set”和“/…”您可以使用regexp解析它们: ^\s*(?:set\s+)?(\w+)(?:\s*=\s*)?"?([^"]*?)"?

解析文本(配置文件)的最佳方法是什么,其中设置变量的“样式”不同:

 var_name = "Content" // comment
 var_name2 = Content // comment
 set var_name3 "Content" // comment
 Var_name4=Content // comment

我只需要变量名和内容,忽略“set”和“/…”

您可以使用regexp解析它们:

^\s*(?:set\s+)?(\w+)(?:\s*=\s*)?"?([^"]*?)"?\s*(?://.*)?$
$1
是变量名,
$2
是值


这有一个错误:如果您将
/
放在引用内容中,它将被视为注释的开头。

为什么您的配置样式语法如此自由?选择一种语法并要求用户遵循它。到目前为止,你研究了什么?为什么不将其编辑成一致的样式,只使用众多ini解析器中的一个呢?将xml用于配置语法自由格式的文本文件配置是解析的噩梦。结构化文本文件(如xml)非常适合于this@Barmar我需要解析多个游戏服务器配置文件,无法更改语法:(.是否必须对所有配置文件使用相同的解析器?谢谢!但我无法使用php进行测试:
preg\u match('/^\s*(?:set\s+)(\w+)(:::\s*=\s*)?([^“]*?)“?\s*(?:/*)$/,$test,$match);
给我警告:未知修饰符“”,在regexp周围使用“^\s*(?:set\s+)(\w+(::=\s*)?”?([^“]*?)“?\s*(?:/.*)?$”:未知修饰符“'在regexp周围使用与
/
不同的分隔符,或者在regexp.Works内转义
/
,谢谢@Barmar:)。