Search code examples
phpregexpreg-matchpreg-match-all

Regular expression to detect line that starts with an asterisk


The code below fails to detect a single instance of the occurance. =O Whats wrong? =\

How do I detect the following lines (which begin with a newline) that begin with an asterisk ? I'm at a loss. This isn't behaving as I expected.

$text ="Nothing here to detect...though it is the first line.
* '' [[test]]
* Another line that starts with an asterisk 
** yet another...though it has two...but who cares about the 2nd one?";


$t = preg_match_all('#^\*.*#', $text, $match);
echo "found=".$t."\n";
print_r($match);

Solution

  • add the m modifier to specify that this has a multi line subject like #^\*.*#m

    http://php.net/manual/en/reference.pcre.pattern.modifiers.php