Re: Regular expressions (regex) question for parsing

From: Date: Tue, 23 Dec 2008 00:42:04 +0000
Subject: Re: Regular expressions (regex) question for parsing
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-284987@lists.php.net to get a copy of this message
Rene Fournier wrote:
Hi, I'm looking for some ideas on the best way to parse blocks of text that is formatted such as:
$sometext %\r\n                        -- good data       $otherstring %\r\n                        -- good data
$andyetmoretext %\r\n                    -- good data
$finaltext                                 -- bad data (missing ending)
Each line should start with a $dollar sign, then some arbitrary text, ends with a percent sign, followed by carriage-return and line-feed. Sometimes though, the final line is not complete. In that case, I want to save those lines too. ....so that I end up with an array like:
$result = array (    "matches" =>
        array (    0 => "$sometext %\r\n",
                1 => "$otherstring %\r\n",
                2 => "$andyetmoretext %\r\n"
                ),
    "non_matches" =>
        array (    3 => "$finaltext"
                )
            );
The key thing here is that the line numbers are preserved and the non-matched lines are saved... Any ideas, what's the best way to go about this? Preg_matc, preg_split or something incorporating explode? ....Rene
Where does the text come from, a text file? client-side textarea? DB? etc.?

« previous php.general (#284987) next »