I ha开发者_如何学Pythonve a variable in PHP which holds a large text (an E-mail header to be precise).
I want to get 2 things out of this header. Here is a sample header:
Received: by mozg.ha.domain.net (Postfix, from userid 0)
id E714C9AEDE; Tue, 29 Mar 2011 10:22:42 +0200 (CEST)
Subject: This is some text before hs34940.somedomain.tld
X-internal-Template: mozg/de/shipping.model
Now, from this text, I want to get 2 variables. One containing hs34940.somedomain.tld and the other one the 4 last digits of the number, in this case 4940. The length of the digits is variable.
I have got this currently in the variable $header, and I'd love to get $number and $name out of it.
Any help is greatly appreciated.
Well here is a simple approach, use regular expressions one to get this line:
Subject: This is some text before hs34940.somedomain.tld
Another inside the first match to get : hs34940.somedomain.tld
And for the number just use a substr if the number of digits is fix.
Useful API:
Preg_match($pattern,$match,$result) - to match patterns against text.
substr($start_indes,$finish_index,$string) - to get a specific part of a string.
For the Subject headline: preg_match('/Subject: [a-zA-Z0-9. ]*/', $header, $headline);
For the domain: preg_match('/[a-z0-9]*[.][a-z0-9]*[.][a-z]*/', $headline[0],$domain);
This isn't too general tho, but for your case will work just fine.
I am not an expert at regex but this might help.
preg_match('/\d{4}\.somedomain.tld.*|\.somedomain1.tld.*/', $header, $arr);
$arr[0] will give you 4940.somedomain.tld
I tried with this tool
Then you may use substr or explode to get the number and the domain name
精彩评论