数组前加echo ‘
- ‘; 出来格式化效果
test_yto.php
<?php
$file = './write_str.xml';
$con = file_get_contents($file);
//XML标签配置
$xmlTag = array(
'starttime',
'endtime',
'school'
);
$arr = array();
foreach($xmlTag as $x) {
preg_match_all("/<".$x.">.*<\/".$x.">/", $con, $temp);
$arr[] = $temp[0];
}
//去除XML标签并组装数据
$data = array();
foreach($arr as $key => $value) {
foreach($value as $k => $v) {
$a = explode($xmlTag[$key].'>', $v);
$v = substr($a[1], 0, strlen($a[1])-2);
$data[$k][$xmlTag[$key]] = $v;
}
}
echo '<pre>';
print_r($data);
write_str.xml
<?xml version="1.0" encoding="utf8"?>
<studentcareer>
<period>
<starttime>2000</starttime>
<endtime>2002</endtime>
<school>培新小学</school>
</period>
<period>
<starttime>2002</starttime>
<endtime>2006</endtime>
<school>览表东阳学校</school>
</period>
<period>
<starttime>2006</starttime>
<endtime>2009</endtime>
<school>惠来慈云实验中学</school>
</period>
<period>
<starttime>2009</starttime>
<endtime>2012</endtime>
<school>惠来一中</school>
</period>
<period>
<starttime>2012</starttime>
<endtime>2016</endtime>
<school>华南师范大学</school>
</period>
</studentcareer>
不加 pre的效果: