I need to extract the name and the e-mail from one data file. the file contains more than 500 lines and I want to extract this two informations almost all the data. I would like to use preg_match_all, but my function doesn't work ...
$chaine =
" -----------------
11/21/12 16:06:54 tcp static-qvn-qvt-127041
MAIL toto1@web.com
NAME tata1
-----------------
11/21/12 16:06:54 tcp static-qvn-qvt-127041
MAIL toto2@web.com
NAME tata2
* -----------------
11/21/12 16:06:54 tcp static-qvn-qvt-127041
MAIL toto3@web.com
NAME tata3
";
//$chaine =" #76:50#89:1#86:50#49:1#84:22";
$motif="/MAIL([a-z]{2,4}+)NAME([a-z]{2,4}+)/";
preg_match_all($motif,$chaine,$out);
$nb=count($out[0]);
for($i=0;$i<$nb;$i++)
{
echo $out[0][$i].'<br/>';
}
"s" modifier add support for multiline.
Your regexp would be look like this:
$motif="/MAIL\s*([a-z]{2,4}).*?NAME\s*([a-z]{2,4})/s";
I feel like regex might be a bit overkill for what you're doing.
Why don't you simply read line by line, explode (documentation here ) with spaces as a delimiter, then check if the first item is "MAIL", or if it is "NAME", then use the second item, otherwise discard the line?
Here's an example code that does it:
foreach(preg_split("/((\r?\n)|(\r\n?))/", $chaine) as $line)
{
$segments = explode(' ', $line);
if($segments[0] === "MAIL")
echo "Found mail " . $segments[1] . "\n";
elseif($segments[0] === "NAME")
echo "Found name " . $segments[1] . "\n";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.