Question

I have a text file with the following contents:

---> 12455  ---> 125  ---> KKK
---> 11366  ---> 120  ---> LLL
---> 12477  ---> 120  ---> YYY

I am using the following PHP code to search the file for "---> 124" and I get the following results:

---> 12455  ---> 125  ---> KKK
---> 12477  ---> 120  ---> YYY

but I want the results to be like this:

---> 12455  
---> 12477  

I want it to return only the first column.

<?php
    $file = 'mytext.txt';
    $searchfor = '---> ' . "124";

    // the following line prevents the browser from parsing this as HTML.
    header('Content-Type: text/plain');

    // get the file contents, assuming the file to be readable (and exist)
    $contents = file_get_contents($file);

    // escape special characters in the query
    $pattern = preg_quote($searchfor, '/');

    // finalise the regular expression, matching the whole line
    $pattern = "/^.*$pattern.*\$/m";

    // search, and store all matching occurences in $matches
    if(preg_match_all($pattern, $contents, $matches)) {
        echo implode($matches[0]);
    } else {
        echo "No matches found";
    }
?>
Was it helpful?

Solution

Change your approach a little bit. Instead of storing the search term and separator in a single string, use two variables.

$sep = '--->';
$searchfor = '124';

$pattern = "/^$sep\s+($searchfor\d+)\s+.*/m";

// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
    echo implode(' ', $matches[1])."\n";
}

Outputs:

12455 12477

Demo.

OTHER TIPS

First of all, seperate your concerns:

  1. Read the file
  2. Parse the content
  3. Search

Using Iterators, you can achieve something great here but it will need a deeper understanding of OOP and the iterator interface. What i'll recommend is a simpler approach:

<?php
//Read the file line by line
$handle = fopen('file.txt', 'r');
while(!foef($handle)){
    $content = fgets($handle);

    //Parse the line
    $content = explode('---> ', $content);

    //Analyse the line
    if($content[1] == 124){
        echo $content[0]."\n";
    }

}
fclose($handle);

That should be it, just adapt it as you see it, i haven't tested the code here!

change "/^.*$pattern.*\$/m" to "/$pattern\d*/i"

and then echo implode($matches[0]); to foreach($matches[0] as $item) echo "$item<br />\r\n";

If the structure is always as you have shown, then:

  1. Read the file line by line;
  2. explode(); each line by space  ;
  3. Read the element [1] of the result;

This seems to be most logical to me. No need for regex in here, because it will work slower then simple explode operation.

Here is an example:

$handle = fopen( 'file.txt', 'r' );
if ( $handle ) {
    while ( ( $line = fgets( $handle ) ) !== false ) {
        $matches = explode( ' ', $line );
        if ( $matches[4] == '124' )
            echo $matches[1] . '<br/>';
    }
}

try this:

--->\s\d{5}

regex is overkill here, a simple explode('--->', $str) and selecting the first element would suffice

$file = file_get_contents('file.txt');
$lines = explode('---> ', $file);
for($i=1; $i<count($lines); $i=$i+3)
if(strpos($lines[$i], '124')!==false)
    $col[$i/3] = /*'--> ' . */$lines[$i];
print_r($col);

That seems to work just fine. Uncomment the comment above if you want the --> included in the output. Also, the resulting $col array is indexed with the row number it is found. Just replace [$i/3] with [] if you don't want that.

Furthering this:

function SearchFileByColumn($contents, $col_num, $search, $col_count = 3) {
    $segs = explode('---> ', $contents);
    for($i=$col_num; $i<count($segs); $i=$i+$col_count)
        if(strpos($segs[$i], $search) !== false)
            $res[] = $segs[$i];
    return $res;
}

$results = SearchFileByColumn($contents, 1, '124');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top