Home >Backend Development >PHP Tutorial > PHP怎么解析这种格式的文件

PHP怎么解析这种格式的文件

WBOY
WBOYOriginal
2016-06-13 13:26:35929browse

PHP如何解析这种格式的文件

XML code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<accounts>
user=15@220 bindlan=none auto_bindmac=yes cn=包 
user=16@613 auto_bindmac=yes bindlan=none cn=包 
user=15@116 bindlan=none cn=- desc=- maxlogin=1

</accounts>

再不用explode的情况下,把上面的格式解析成数组,如 $a[accounts][user]='15@220'这种格式

explode是下下策,众多PHP函数,有一个能解析此类格式文件吗?类似于parse_ini_file这种函数

------解决方案--------------------
PHP code
$s = 
user=15@220 bindlan=none auto_bindmac=yes cn=包 
user=16@613 auto_bindmac=yes bindlan=none cn=包 
user=15@116 bindlan=none cn=- desc=- maxlogin=1
TXT;

$s = str_replace(' ', "\n", $s);
$s = str_replace('', "]", $s);

$p = parse_ini_string($s, 1);
print_r($p);
<br><font color="#e78608">------解决方案--------------------</font><br>
PHP code
$s = 
user=15@220 bindlan=none auto_bindmac=yes cn=包 
user=16@613 auto_bindmac=yes bindlan=none cn=包 
user=15@116 bindlan=none cn=- desc=- maxlogin=1
TXT;

preg_match_all("/[\S]+=[^\s]*/",$s,$c);
print_r($c);
<br><font color="#e78608">------解决方案--------------------</font><br>楼上2种方法都可以<br><br><br>郑州PHP.Net<br>http://www.zzphp.net/
<br><font color="#e78608">------解决方案--------------------</font><br>parse_ini_string 第一次看到呢. <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn