I’m trying to extract a value from a multiline pattern with PHP and preg_match.
The pattern I’m searching for within the string I’m passing to preg_match($regex, $string, $the_match):
Latitude:</td>
<td class="formCell">
40-45-40.205 N
</tr>
I know that if it were all on one line like so:
Latitude:</td><td class="formCell">40-45-40.205 N</tr>
Then the following would be valid and it would properly extract the value:
/Latitude:<\/td><td class="formCell">(.*?)<\/tr>/
However, since the pattern I’m looking for has multiple lines the above regex doesn’t work. I’m getting the initial string I’m passing to preg_match() via file_get_contents($url) so I’m at the mercy of the remote content to some extent. Any help would be much appreciated!
Use
[\s\S]instead of...is a wildcard but does not include whitespace – including line break – characters.[\s\S]simply says "match all space and non-space characters" (i.e. anything at all).Note I also allowed for optional space characters after
</td>.(Sidenote: the HTML is invalid – closing a table row before closing the table cell.)