0

I do a file_get_contents to get a long file and I would like to find the line (there is only one) which starts with "MyParam" (for example). I don't want to cut the file line by line, I want to do it with a preg_match only (or equivalent). I've tried many things but no success. Thanks in advance

FILE CONTENT

//some text
//some text
//MyParam "red"
//some text
MyParam "blue"
Some text
// Some text

And I would like to get MyParam "blue" only.

Peter Estiven
  • 414
  • 1
  • 7
  • 16

5 Answers5

4

If you must use a regex, you can do it as follows:

preg_match('/^MyParam[^\r\n]*/m', $text, $matches);
var_dump($matches[0]);

Explanation:

  • [^\r\n] - a character class that matches any characters other than a \r or \n.
  • m - multiline modifier, which changes the meaning of ^ from "assert position at the beginning of the string" to "assert position at the beginning of each line".

Here, $matches[0] will contain the complete matched string (which in this case, is what you want).

Output:

string(14) "MyParam "blue""

Demo

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
3

Using file_get_contents is not the best choice here since you can read the file line by line

$handle = fopen("yourfile.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        if (strpos($line, 'MyParam')===0) {
            echo 'found';
            break;
        }
    }
} else {
    echo 'error opening the file';
} 
fclose($handle);

Note: if you need to extract a parameter value between quotes you can use:

$value = explode('"', $line, 3)[1];
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
2

No need of regexp:

$pos = strpos($filecontent, "\nMyParam");
$endline = strpos($filecontent, "\n", $pos + 1);
$line = substr($filecontent, $pos + 1, $endline - $pos - 1) ;

Not tested, there is maybe an offset of +-1 in the string but it gives you the idea.

Edit : If you are sure that "MyParam" does not appears elsewhere in the file, you can remove the first "\n".

Holt
  • 36,600
  • 7
  • 92
  • 139
1

You could use strpos() to find the position of the first occurrence of a substring in a string.

http://nl1.php.net/strpos

R Pelzer
  • 1,188
  • 14
  • 34
1

I would consider a couple of different approaches here. The most important thing to consider is that you don't need to read the entire file into memory via file_get_contents(). I would be inclined to just use shell commands to do this:

$file = '/path/to/file';
$search_pattern = '^MyParam';
$file_safe = escapeshellarg($file);
$pattern_safe = escapeshellarg($search_pattern);
$result = exec('grep ' . $pattern_safe . ' ' . $file_safe);

Alternately, you can read through the file one line at a time looking for the match:

$result = '';
$file = '/path/to/file';
$search = 'MyParam'; // note I am not using regex here as this is unnecessary in your case
$handle = fopen($file, 'r');
if ($handle) {
    while($line = fgets($handle) !== false) {
        if (strpos($line, $search) === 0) { // exact match is important here
            $result = $line;
            break;
        }
    }
    fclose($handle);
}
Mike Brant
  • 70,514
  • 10
  • 99
  • 103