Zachary Lassiter
Zachary Lassiter

Reputation: 181

PHP preg_match find line in Multiple Line Variable that starts with

$stxt = "000
WGCA82 TJSJ 260200
FLSSJU

FLOOD ADVISORY
NATIONAL WEATHER SERVICE SAN JUAN PR
1000 PM AST FRI NOV 25 2011

PRC013-017-039-054-141-260209-
/O.EXP.TJSJ.FA.Y.0568.000000T0000Z-111126T0200Z/
/00000.N.ER.000000T0000Z.000000T0000Z.000000T0000Z.OO/
BARCELONETA PR-CIALES PR-FLORIDA PR-UTUADO PR-ARECIBO PR-
1000 PM AST FRI NOV 25 2011

...THE FLOOD ADVISORY WILL EXPIRE AT 1000 PM AST FOR ARECIBO...
UTUADO...FLORIDA...CIALES AND BARCELONETA MUNICIPALITIES...

&&

LAT...LON 1842 6666 1844 6656 1830 6657 1831 6672

$$
GV";

preg_match("(NATIONAL WEATHER SERVICE.*\n)", $stxt, $ofc);
print_r($ofc);

It returns an empty array. Help... I want it to match NATIONAL WEATHER SERVICE SAN JUAN PR (or any other city where a National Weather Service office is)

Upvotes: 1

Views: 278

Answers (3)

hakre
hakre

Reputation: 198119

It looks like that you're running over an error here. Enable error reporting and display errors to find out. Something like:

Warning: preg_match(): Internal pcre_fullinfo() error -3 on line ...

When that error happens, the matches array will be empty. To test if your regular expression failed:

# Report all and display error messages
error_reporting(~0);
ini_set('display_errors', 1);

$result = preg_match("(NATIONAL WEATHER SERVICE.*\n)", $stxt, $ofc);

# Check return value for an error
if (FALSE === $result)
{
    throw new RuntimeException('preg_match() failed.');
}

BTW, your code-example works fine for me. However some PHP installations behave similar to what you describe, see codepad.org example.

Upvotes: 1

codaddict
codaddict

Reputation: 455302

As already mentioned you need to add delimiters to your regex.
In your existing code ( and ) are acting as delimiters!! But you meant to use them as capturing parenthesis. Since there are no capturing parenthesis in your regex nothing is being captured.

Upvotes: 0

Clive
Clive

Reputation: 36955

You need to add delimiters to your regex, e.g.

preg_match("/(NATIONAL WEATHER SERVICE.*\n)/", $stxt, $ofc);

Upvotes: 2

Related Questions