求正则大侠帮助一下
HTTP/1.1 200 OKServer: nginx/0.7.68Date: Sat, 02 Mar 2013 07:44:57 GMTContent-Type: application/rss+xml; charset=utf-8Transfer-Encoding: chunkedConnection: keep-alive3ab4<?xml version="1.0" encoding="utf-8"?><rss version="2.0"> <channel> <title>CSDN 移动频道(资讯)</title> <link>http://mobile.csdn.net</link> <description>开放平台</description> <lastBuildDate>Sat, 02 Mar 2013 15:44:50</lastBuildDate> <item> <title>移动周报:我为什么弃用Objective-C而选择RubyMotion?</title> <link>http://www.csdn.net/article/2013-03-01/2814309-mobile-weekly-choose-rubymotion-for-ios-dev</link> <description><![CDATA[我们为您精心准备了CSDN移动频道本周最精彩的热点新闻,开发者直言RubyMotion优与劣,讲述为何弃用Objective-C而选择RubyMotion;出生贫苦的Minecraft开发者Notch大神特立独行,始终秉承开放的游戏开发理念;移动互联网大潮来袭,国家级老牌媒体China Daily将怎样拥抱新媒体时代?]]></description> <author>唐小引</author> <pubDate>Fri, 01 Mar 2013 23:41:01</pubDate> <guid>http://www.csdn.net/article/2013-03-01/2814309-mobile-weekly-choose-rubymotion-for-ios-dev</guid> </item> <item> <title>遍地撒网,重点培养,King.com颠覆性游戏商业模式</title> <link>http://www.csdn.net/article/2013-02-27/2814282-king.com-game-launch</link> <description><![CDATA[在App Store的Top Grossing排行榜中,Candy Crush Saga已经排到第二位,仅次于Supercell的Clash of Clans,其开发公司king.com也因此名声大噪,并在移动端打败Zynga,它的成功离不开公司商业模式的创新:发布三部曲+高质游戏+顺势而行。]]></description> <author>陈徐天九</author> <pubDate>Fri, 01 Mar 2013 10:49:52</pubDate> <guid>http://www.csdn.net/article/2013-02-27/2814282-king.com-game-launch</guid> </item> ...
我想用一个正则把上面的内容分成两部分
从空白行分开的两部分为:
1.响应基本信息
HTTP/1.1 200 OKServer: nginx/0.7.68Date: Sat, 02 Mar 2013 07:44:57 GMTContent-Type: application/rss+xml; charset=utf-8Transfer-Encoding: chunkedConnection: keep-alive
2.响应内容
3ab4<?xml version="1.0" encoding="utf-8"?><rss version="2.0"> <channel> <title>CSDN 移动频道(资讯)</title> <link>http://mobile.csdn.net</link> <description>开放平台</description> <lastBuildDate>Sat, 02 Mar 2013 15:44:50</lastBuildDate> <item> <title>移动周报:我为什么弃用Objective-C而选择RubyMotion?</title> <link>http://www.csdn.net/article/2013-03-01/2814309-mobile-weekly-choose-rubymotion-for-ios-dev</link> <description><![CDATA[我们为您精心准备了CSDN移动频道本周最精彩的热点新闻,开发者直言RubyMotion优与劣,讲述为何弃用Objective-C而选择RubyMotion;出生贫苦的Minecraft开发者Notch大神特立独行,始终秉承开放的游戏开发理念;移动互联网大潮来袭,国家级老牌媒体China Daily将怎样拥抱新媒体时代?]]></description> <author>唐小引</author> <pubDate>Fri, 01 Mar 2013 23:41:01</pubDate> <guid>http://www.csdn.net/article/2013-03-01/2814309-mobile-weekly-choose-rubymotion-for-ios-dev</guid> </item> <item> <title>遍地撒网,重点培养,King.com颠覆性游戏商业模式</title> <link>http://www.csdn.net/article/2013-02-27/2814282-king.com-game-launch</link> <description><![CDATA[在App Store的Top Grossing排行榜中,Candy Crush Saga已经排到第二位,仅次于Supercell的Clash of Clans,其开发公司king.com也因此名声大噪,并在移动端打败Zynga,它的成功离不开公司商业模式的创新:发布三部曲+高质游戏+顺势而行。]]></description> <author>陈徐天九</author> <pubDate>Fri, 01 Mar 2013 10:49:52</pubDate> <guid>http://www.csdn.net/article/2013-02-27/2814282-king.com-game-launch</guid> </item> ...
回复讨论(解决方案)
你不读取http头不就行了?
你不读取http头不就行了?
我需要头信息呀。因为Transfer-Encoding: chunked
$arr=preg_split('/(?<=Connection: keep-alive)\s+/s',$s);print_r($arr);
我需要头信息呀。因为Transfer-Encoding: chunked 这个 Transfer-Encoding: chunked 对你而言,毫无意义
PHP code?12$arr=preg_split('/(?<=Connection: keep-alive)\s+/s',$s);print_r($arr);
并不是所有的响应头信息都是Connection结束.
引用 2 楼 pktmalaha 的回复:我需要头信息呀。因为Transfer-Encoding: chunked这个 Transfer-Encoding: chunked 对你而言,毫无意义
我当然需要了.因为如果是chunked需要unchunk
引用 2 楼 pktmalaha 的回复:我需要头信息呀。因为Transfer-Encoding: chunked这个 Transfer-Encoding: chunked 对你而言,毫无意义
头信息我是一定需要的.因为我的代码需要它作很多事情.
引用 4 楼 xuzuning 的回复:引用 2 楼 pktmalaha 的回复:我需要头信息呀。因为Transfer-Encoding: chunked这个 Transfer-Encoding: chunked 对你而言,毫无意义
我当然需要了.因为如果是chunked需要unchunk 那就是说你得自己分析返回的头了
如果这样,就不应该是正则的事情了!
你需要逐行读取头信息,直到读到空行(头结束)
这不是一件正则可以胜任的工作
从第一个空行截取两部分不就行了?
然后头部自己再分析
从第一个空行截取两部分不就行了?
然后头部自己再分析
我对正则不怎么会.就是要从空行截取.你能提供一下正则吗
本帖最后由 xuzuning 于 2013-03-04 08:29:12 编辑
引用 6 楼 pktmalaha 的回复:引用 4 楼 xuzuning 的回复:引用 2 楼 pktmalaha 的回复:我需要头信息呀。因为Transfer-Encoding: chunked这个 Transfer-Encoding: chunked 对你而……
我需要头信息来作头信息的工作,需要正文来作正文的工作.而我用socket得来的内容两者混在一起。现在是否可用正则把两者分开呢?我需要你们提供一个正则表达式.而不是告诉我我需要什么
explode("\r\n\r\n", $str, 1);
explode("\r\n\r\n", $str, 1); 看清了我需要的是正则,explode的第一参数是正则吗
如果你得到的响应数据是响应头+响应主题,http响应头以两个空行结尾。可以作为分隔的依据。
另外一种方式,有很多类库可以单独获取响应头和响应主题。如httplib
引用 12 楼 helloyou0 的回复:explode("\r\n\r\n", $str, 1);看清了我需要的是正则,explode的第一参数是正则吗
能不用正则建议不要用,正则并不高效。
用换行截取成两个部份不就可以了吗
如果非要用正则,试试下面的写法
$arr = preg_split('#^[\r\n]#m', $text);print_r($arr);
非要用正则这个...是什么原因?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

RESTAPI design principles include resource definition, URI design, HTTP method usage, status code usage, version control, and HATEOAS. 1. Resources should be represented by nouns and maintained at a hierarchy. 2. HTTP methods should conform to their semantics, such as GET is used to obtain resources. 3. The status code should be used correctly, such as 404 means that the resource does not exist. 4. Version control can be implemented through URI or header. 5. HATEOAS boots client operations through links in response.

In PHP, exception handling is achieved through the try, catch, finally, and throw keywords. 1) The try block surrounds the code that may throw exceptions; 2) The catch block handles exceptions; 3) Finally block ensures that the code is always executed; 4) throw is used to manually throw exceptions. These mechanisms help improve the robustness and maintainability of your code.

The main function of anonymous classes in PHP is to create one-time objects. 1. Anonymous classes allow classes without names to be directly defined in the code, which is suitable for temporary requirements. 2. They can inherit classes or implement interfaces to increase flexibility. 3. Pay attention to performance and code readability when using it, and avoid repeatedly defining the same anonymous classes.
