2

I am currently attempting to drag some information out of a .txt file using a php script. I have been reading about regex's and thought this would be ideal. To give you some idea the format of the text in the .txt file is as follows:

Data Rate: 20 Hz

Digital I/O Channels: 
CH1_IN,0,CH2_OUT,1,CH3_IN,0,CH4_OUT,1,CH5_IN,0,CH6_IN,0,CH7_IN,0,CH8_OUT,0,CH9_IN,0,
CH10_IN,0,CH11_OUT,1,CH12_IN,0,CH13_IN,0,CH14_IN,0,CH15_IN,0,CH16_IN,0,

QEA: Enabled

I am trying to pull out the following detail for each channel:

CH(number)_(IN or OUT),(integer)

As described in various posts and some tutorials I have tried using preg_split but haven't been able to get it to work as I want. My understanding is that something like that shown below should work, although it is likely I have not used it correctly:

$log_file_data = file_get_contents('Log.txt');
$channel_detail = preg_split("/CH[0-9]{2}_[A-Z],[0-1]{1}/",$log_file_data);

My intention is that this would split the text nicely into portions as described earlier but as expected it just pretty much spews out the complete text file. Am I using the correct method or does it not suit what I am looking to achieve?

Any guidance would be appreciated.

2 Answers 2

2

You don't need preg_split actually but preg_match_all with an improved regex:

$line = <<< EOF
CH1_IN,0,CH2_OUT,1,CH3_IN,0,CH4_OUT,1,CH5_IN,0,CH6_IN,0,CH7_IN,0,CH8_OUT,0,CH9_IN,0,
CH10_IN,0,CH11_OUT,1,CH12_IN,0,CH13_IN,0,CH14_IN,0,CH15_IN,0,CH16_IN,0,
EOF;

if (preg_match_all('/CH([0-9]+)_(IN|OUT),([01])/', $line, $arr))
   print_r($arr);

Your channel #, IN/OUT and next number is available in groups #1, #2 and #3

Sign up to request clarification or add additional context in comments.

3 Comments

Hey anubhava, much neater than what I tried to do and seems to almost work. I'm not sure I can use the $line = <<< EOF bit though as the text file is read on opening a browser which populates various bits so I wont be able to pull the data out before using preg_match_all. When I run this replacing $line with $log_file_data it pulls out exactly what I need and then goes on to seemingly break the data down further e.g:Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] etc.... What do you think I'm doing wrong?
$line = <<< EOF was just an example, you need to replace $line with the line of file you're reading and put this in a loop processing file line by line.
Ahh, I think I understand, I did some reading up on preg_match_all and as I understand it, multiple arrays can be created to produce all matches of the 'match pattern'. Therefore when I only print_r'd $arr[0] I got exactly what I need. Thanks
0

You really don't need regex at all. Exploding on ',' will yield an array that is Channel names for all odd indexes, and every even number will contain an integer that belong to the last index.

Cheers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.