Obtain data in xml using a regular expression

Source: Internet
Author: User
Obtain data in xml using a regular expression
20110524

13.82

13.94

13.79

13.85


20110525

13.82

13.86

13.58

13.60



This is part of the xml file. it is to extract the values following the value tag one by one from it. it is better to know several records.
It can be applied to a defined function, such
Function value ($ a, $ B, $ c, $ d, $ n ){
.......
}
For (I = 1; I <(number of records); I ++ ){
Value ($ a, $ B, $ c, $ d, $ I );
}
Cycles:
First implementation of value (13.82, 13.94, 13.79, 13.85, 1)
Second implementation value (13.82, 13.86, 13.58, 13.60, 2)


Reply to discussion (solution)

A regular expression is not required. you can use the function to obtain xml data .. Thank you!

Php provides a dedicated method

$s =<<< XML
 
  
   
    20110524
   
  
  
   
    13.82
   
  
  
   
    13.94
   
  
  
   
    13.79
   
  
  
   
    13.85
   
  
 
 
  
   
    20110525
   
  
  
   
    13.82
   
  
  
   
    13.86
   
  
  
   
    13.58
   
  
  
   
    13.60
   
  
 XML;$x = simplexml_load_string("
 
  $s
 ");foreach($x->Record as $node) {  $t = array();  foreach($node->Item as $v) $t[] = current($v->Value);  print_r($t);}
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)

$count=preg_match_all('/
 
  (.+?)<\/Record>/is',$str,$m);echo $count;preg_match_all('/
  
   (.+?)<\/Value>/is',$str,$values);echo "
   
";print_r($values[1]);echo "
";/*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)*/

Thank you for the two great gods upstairs. The problem has been solved according to your methods ~~ Hey

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.