PHP Regular Expression problem when trying to match numbers
I have an issue with regular expressions in PHP.
I'm trying to find a match for some header data that I received via CURL. However, my regex is matching two lines, instead of one.
Below is my regex pattern:
/^(http\/\d\.\d)\s+(100)\s+([\w\-\s\/\'\开发者_如何学C"\(\)\\\.]+)$/im
And here is the string I'm trying match:
HTTP/1.1 100 Continue
HTTP/1.1 201 Created
Content-Length: 118
Content-Type: text/html; charset=UTF-8
Etag: 8c59b7e37f672374c61245c8115f53d0
Last-Modified: Tue, 16 Aug 2011 20:24:10 GMT
X-Trans-Id: txd6cbafcec90d4e30b2a108ff3157c1b1
Date: Tue, 16 Aug 2011 20:24:10 GMT
It's supposed to match the first line but I'm getting a match for both 100 and 201. I can't understand why it's doing this.
I'm using preg_match by the way.
Any ideas as to how I can solve this? Thanks.
Update:
The multiline modifier is orthogonal to the real cause. Your problem is that the character class definition (That between the square brackets) includes \s
, which matches any whitespace character, including linebreaks. Replace it with an actual space in this case, to match just that.
As an aside, if you have access to install extensions on your server, you might want to use the pecl_http
extension for parsing http, rather than doing it by hand.
@troelskn is right, however you can also ask CURL to tell you the HTTP response code:
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
EDIT: Instead of:
/^(http\/\d\.\d)\s(100)\s([\w\-\s\/\'\"\(\)\\\.]+)$/im
Try (RegexPal):
/^(http\/\d\.\d)\s(100)\s([\w\-\s\/\'\"\(\)\\\.]+?)$/im
精彩评论