php - preg_match_all does not give results -


i don't know why when execute script doesn't display lines correctly. gives last line of variable lines no input under it. overlooking something?

<?php     $stop = '06bah';     $input = file_get_contents('file1.txt');      $lines = file('file2.txt');       ($i=0; $i < count($lines); $i++) {          preg_match_all("/^($lines[$i].*)$stop/msu", $input, $matches);     } ?> 

file1:

06bah  toi00123-11-134-b                        op_signage                                       10   05  6       00132-12-172-a                4                                                                                    pon 05  7       00127-22-683-a                3                                                                                   pon 05  9       00927-62-133-a                11                                                                                    pon 05  18      00227-72-542-a                8                                                                                   pon 06bah   toi00877-27-836-c                        op_signage                                      10  05  122     00238-92-963-a                3                                                                                    pon 05  173     00124-65-832-a                2                                                                                    pon 06bah   toi00112-54-980-b                        op_signage                                      10  

file2:

toi00123-11-134-b toi00112-54-980-b 

output:

06bah   toi00123-11-134-b                        op_signage                                       10   05  6       00132-12-172-a                4                                                                                    pon 05  7       00127-22-683-a                3                                                                                   pon 05  9       00927-62-133-a                11                                                                                    pon 05  18      00227-72-542-a                8                                                                                   pon 06bah   toi00112-54-980-b                        op_signage                                       10  

your problem is, although storing results in $matches overwriting $matches everytime loop iterates.

you should go along lines of this:

<?php $stop = '06bah'; $input = file_get_contents('file1.txt');  $lines = file('file2.txt');  $caught = [];   ($i=0; $i < count($lines); $i++) {      preg_match_all("/(".trim($lines[$i]).".*)".trim($stop)."/msu", $input, $matches);     foreach($matches $key => $value) {         $caught[$i] = $value;     } }  var_dump($caught);  ?> 

also, note change regular expression - i'm not best regex after testing original expression wasn't matching anchor ^.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -