Re: Searching a string

From: Date: Mon, 10 Jul 2000 14:04:30 +0000
Subject: Re: Searching a string
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-5720@lists.php.net to get a copy of this message
"J. Geusebroek" wrote: > > Hi there, > > I have the following problem / question: > > I'm trying to parse an email message using PHP. The email message always > contains some > static text for example: > > From: "Mr.X" <Mr.X@linux.org> > To: <i@dont.know> > Subject: qweqwe > > I have the complete file spit in lines in my PHP script. How can i extract > the values > behind From: To: and Subject: ? I was thinking about using 'ereg' to > identify the lines > and then use 'substr' to get the rest of the string. > > I don't really understand how to use the ereg function, can someone please > help? > Hi! You can do this with 'ereg' or 'explode' like in following examples (i suppose that lines are in array '$lines'): <? /*-- hash for headers we search --*/ $hdr = array(); /*-- ereg-example ... --*/ while ( list( $nr, $line ) = each( $lines )) { /*-- find header and value from $line --*/ if ( ereg( '^(.+): (.+)$', $line, $parts)) { /*-- $parts[1] is header, $parts[2] is value --*/ $hdr[$parts[1]] = $parts[2]; } } /*-- ... or explode-example --*/ while ( list( $nr, $line ) = each( $lines )) { /*-- split line like this --*/ list( $header, $value ) = explode( ': ', $line ); $hdr[$header] = $value; } ?> Now $hdr contains these lines: $hdr['From'] => '"Mr.X" <Mr.X@linux.org>' etc... Hope this helps. -- Pavel a.k.a. Papi

« previous php.general (#5720) next »