Re: Fw: [PHP] Re: Regular expression
| From: | Red Wingate | Date: | Wed, 30 Jun 2004 11:57:33 +0000 |
| Subject: | Re: Fw: [PHP] Re: Regular expression | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-189288@lists.php.net to get a copy of this message | ||
First of all check who u credit :p
Secondly why don't you just try to fix it yourself?
There was just a typo in the regexp:
- #^from\s*:\s*([^>]+)(<([^>]+)>)?#si
+ #^from\s*:\s*([^<]+)(<([^>]+)>)?#si
Hopefully this will help you even more:
<?php
/**
* @param string $runlevel
* @return mixed
*/
function fetchSender ( $mailText ) {
$mailText = explode( "\n" , $mailText ) ;
foreach ( $mailText AS $mailLine ) {
$mailLine = trim ( $mailLine ) ;
if ( strtoupper ( substr ( $mailLine , 0 , 4 ) ) == 'FROM' ) {
preg_match ( '#^\s*FROM\s*:\s*(([^<]+)(\<([^>]+)\>)?)#si' ,
$mailLine , $parts );
break ;
}
}
if ( is_array ( $parts ) === FALSE ) {
return FALSE ;
} else {
return array ( 'name' => isset ( $parts[2] ) ? $parts[2] : 'unknown' ,
'from' => isset ( $parts[1] ) ? $parts[1] : 'unknown' ,
'mail' => isset ( $parts[4] ) ? $parts[4] : 'unknown' ) ;
}
}
/**
* @param array $strings
* @return void
*/
function debug_FetchSender ( $strings ) {
echo '<pre>';
foreach ( $strings AS $string ) {
$sender = fetchSender ( $string );
$sender = array_map ( 'htmlspecialchars' , $sender ) ;
print_r ( $sender );
}
echo '</pre>';
}
debug_FetchSender ( array ( 'FROM: Red Wingate <redeye@erisx.de>' ,
'FROM: Red Wingate' ,
'FROM: redeye@erisx.de' ) ) ;
?>
[...]
>> Thanks Aiden for ur help
>>
>> i used ur code and i got
>>
>> name as Red Wingate<redeye@erisx.de
>> and no email.
>>
>> So pls correct the code and send me such that i get name and email
>> separately.
>>
>> Regards
>> syed
[...]