matching across new lines

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    matching across new lines

    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_te mpl_file, "r");
    while($line=tri m(fread($fp, 102400))) {
    $file_contents .= $line;
    }
    fclose($fp);
    $match = array();
    $pattern = '/' . EMAIL_TEXT_STAR T_TOKEN . '(.*)' .
    EMAIL_TEXT_END_ TOKEN . '/';
    preg_match($pat tern, $file_contents, $match);

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

    #2
    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.comwrot e:
    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_te mpl_file, "r");
    while($line=tri m(fread($fp, 102400))) {
    $file_contents .= $line;
    }
    fclose($fp);
    $match = array();
    $pattern = '/' . EMAIL_TEXT_STAR T_TOKEN . '(.*)' .
    EMAIL_TEXT_END_ TOKEN . '/';
    preg_match($pat tern, $file_contents, $match);
    >
    Anyway, any advice you have is greatly appreciated, - Dave

    Comment

    Working...