Home > Backend Development > PHP Tutorial > 正则表达式 获取xml中的数据

正则表达式 获取xml中的数据

WBOY
Release: 2016-06-23 13:26:38
Original
1267 people have browsed it


20110524

13.82

13.94

13.79

13.85


20110525

13.82

13.86

13.58

13.60



这是xml的文件部分内容,就是从里面一个一个的取出value标签后面的值,最好还能得知有几个record。
就是可以应用到自己定义的函数里面,比如
function value($a,$b,$c,$d,$n){
.......
}
for(i=1;i value($a,$b,$c,$d,$i);
}
循环次数:
第一次实现value(13.82,13.94,13.79,13.85,1)
第二次实现value(13.82,13.86,13.58,13.60,2)


回复讨论(解决方案)

不一定需要正则表达式,只要能获取xml的数据,能够使用到函数里面就可以的。。谢谢各位大神了!

php 提供了专用方法

$s =<<< XML<Record><Item Id="1"><Value>20110524</Value></Item><Item Id="7"><Value>13.82</Value></Item><Item Id="8"><Value>13.94</Value></Item><Item Id="9"><Value>13.79</Value></Item><Item Id="11"><Value>13.85</Value></Item></Record><Record><Item Id="1"><Value>20110525</Value></Item><Item Id="7"><Value>13.82</Value></Item><Item Id="8"><Value>13.86</Value></Item><Item Id="9"><Value>13.58</Value></Item><Item Id="11"><Value>13.60</Value></Item></Record>XML;$x = simplexml_load_string("<root>$s</root>");foreach($x->Record as $node) {  $t = array();  foreach($node->Item as $v) $t[] = current($v->Value);  print_r($t);}
Copy after login
Array(    [0] => 20110524    [1] => 13.82    [2] => 13.94    [3] => 13.79    [4] => 13.85)Array(    [0] => 20110525    [1] => 13.82    [2] => 13.86    [3] => 13.58    [4] => 13.60)
Copy after login

$count=preg_match_all('/<Record>(.+?)<\/Record>/is',$str,$m);echo $count;preg_match_all('/<Value>(.+?)<\/Value>/is',$str,$values);echo "<pre class="brush:php;toolbar:false">";print_r($values[1]);echo "
Copy after login
";/*2Array( [0] => 20110524 [1] => 13.82 [2] => 13.94 [3] => 13.79 [4] => 13.85 [5] => 20110525 [6] => 13.82 [7] => 13.86 [8] => 13.58 [9] => 13.60)*/

谢谢楼上的两位大神,问题按照你们的方法已经解决了~~嘿嘿

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template