Connecting Tech Pros Worldwide Forums | Help | Site Map

matching across new lines

laredotornado@zipmail.com
Guest
 
Posts: n/a
#1: Mar 4 '08
Hi,

I'm using PHP 4.4.4. I'm reading the contents of a file and then
trying to extract contents between two string tokens. Problem is,
tokens occur on different lines. It seems that regular expressions
only match one line at a time. Is there any way to make them span an
entire string? Here's what I got

$file_contents = "";
$fp = fopen($email_templ_file, "r");
while($line=trim(fread($fp, 102400))) {
$file_contents .= $line;
}
fclose($fp);
$match = array();
$pattern = '/' . EMAIL_TEXT_START_TOKEN . '(.*)' .
EMAIL_TEXT_END_TOKEN . '/';
preg_match($pattern, $file_contents, $match);

Anyway, any advice you have is greatly appreciated, - Dave

petersprc
Guest
 
Posts: n/a
#2: Mar 4 '08

re: matching across new lines


Hi,

Try the dot all option (/s) to have the dot metachar match newlines as
well.

HTH,

John Peters

On Mar 4, 4:07 pm, "laredotorn...@zipmail.com"
<laredotorn...@zipmail.comwrote:
Quote:
Hi,
>
I'm using PHP 4.4.4. I'm reading the contents of a file and then
trying to extract contents between two string tokens. Problem is,
tokens occur on different lines. It seems that regular expressions
only match one line at a time. Is there any way to make them span an
entire string? Here's what I got
>
$file_contents = "";
$fp = fopen($email_templ_file, "r");
while($line=trim(fread($fp, 102400))) {
$file_contents .= $line;
}
fclose($fp);
$match = array();
$pattern = '/' . EMAIL_TEXT_START_TOKEN . '(.*)' .
EMAIL_TEXT_END_TOKEN . '/';
preg_match($pattern, $file_contents, $match);
>
Anyway, any advice you have is greatly appreciated, - Dave
Closed Thread