jreed121
jreed121

Reputation: 2097

PHP REGEX preg_match_all every line after a particular line

Here is the sample string with my regex and code:

$str = "Supp Fees:
----------
Oral Glucose
Glucagon
OXYGEN";

$ptn = "/----------(?:\r\n(.+))+/m";
preg_match_all($ptn,$str,$matches);

echo"<pre>";
print_r($matches);
echo"</pre>";

I'm trying to match every line after '----------' the pattern above only returns the first line (Oral Glucose). I can repeat the '\r\n(.+)' part and return another line but there is no telling how many lines there will be.

Thanks!

Upvotes: 2

Views: 364

Answers (2)

Demian Brecht
Demian Brecht

Reputation: 21378

<?php

$str = "Supp Fees:
----------
Oral Glucose
Glucagon
OXYGEN";


$str = explode('----------', $str);
preg_match_all("/[^\r\n].*/", $str[1], $matches);

echo"<pre>";
print_r($matches);
echo"</pre>";

?>

?

Upvotes: 1

Ben Rowe
Ben Rowe

Reputation: 28721

You could do this without regex:

$data = substr($str, strpos($str, '----------') + 10);
$matches = explode("\r\n", $data);

Upvotes: 2

Related Questions