troubleshooting Question

Parse a file with Regular Expression with PHP

Avatar of GeorgeTowers
GeorgeTowers asked on
PHPRegular Expressions
3 Comments1 Solution83 ViewsLast Modified:
I'm trying to parse a file and I'm using regular expression to split in chunks the records, below is so far what I have:
 $file_url = 'https://raw.githubusercontent.com/unitedstates/congress-legislators/master/committee-membership-current.yaml';

  // get raw file contents
  $raw_data = file_get_contents($file_url);

  // arrays for each record
  $id = $id_fields = $name = $name_fields = array();
  
  // get record chunks
  preg_match_all('/(.*?)\n[A-Z0-9]/sm', $raw_data, $record_chunks);

  var_dump($record_chunks);

The first chunk is getting all the info, the second value from the first array is loosing the first letter, example:
array (size=2)
  0 => 
    array (size=212)
      0 => string 'HLIG:
- name: Devin Nunes
  party: majority
.......
- name: Jeff Miller
  party: majority
.......
- name: K. Michael Conaway
  party: majority
.......
      1 => string 'LIG01:
- name: Frank A. LoBiondo
  party: majority
......
- name: K. Michael Conaway
  party: majority
..........
      2 => string 'LIG02:
- name: Lynn A. Westmoreland
  party: majority
 ....
      3 => string 'LIG03:
- name: Thomas J. Rooney
  party: majority
.......

As you can see just the first element (HLIG) is correct the consecutive ones  (LIG01, LIG0X) are losing the first letter (H), what am I doing wrong?

Thanks.
ASKER CERTIFIED SOLUTION
Dan Craciun
IT Consultant

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros