Avatar of GeorgeTowers
GeorgeTowers asked on

Parse a file with Regular Expression with PHP

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);


Open in new window

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

Open in new window

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?

Regular ExpressionsPHP

Avatar of undefined
Last Comment
Dan Craciun

8/22/2022 - Mon
Dan Craciun

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Thanks, works like a charm.
Dan Craciun

You're welcome.

Glad I could help!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes