treeleaf20 Posted October 13, 2009 Share Posted October 13, 2009 All, Say i have the following data: Tue, 13 Oct 2009 10:52:37 -0700 (PDT) Date: Tue, 13 Oct 2009 13:52:37 -0400 Message-ID: Subject: Test Subject From: Test User How can I search the whole data for the Subject: and then read in the subject into a variable until it gets to the new line character? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/177571-find-text-in-data-and-then-read-until-new-line/ Share on other sites More sharing options...
.josh Posted October 13, 2009 Share Posted October 13, 2009 preg_match('~^Subject: (.*)~',$content,$match); print_r($match); edit: that assumes that there is also a \n before "Subject: ..." you may need to remove that ^ Quote Link to comment https://forums.phpfreaks.com/topic/177571-find-text-in-data-and-then-read-until-new-line/#findComment-936261 Share on other sites More sharing options...
JAY6390 Posted October 14, 2009 Share Posted October 14, 2009 preg_match('/^Subject: (.*?)$/m', $content, $match); $subject = $match[1]; Quote Link to comment https://forums.phpfreaks.com/topic/177571-find-text-in-data-and-then-read-until-new-line/#findComment-936480 Share on other sites More sharing options...
.josh Posted October 14, 2009 Share Posted October 14, 2009 preg_match('/^Subject: (.*?)$/m', $content, $match); $subject = $match[1]; the dot normally does not match \n so it is not necessary to have all that extra junk in there. Quote Link to comment https://forums.phpfreaks.com/topic/177571-find-text-in-data-and-then-read-until-new-line/#findComment-936500 Share on other sites More sharing options...
JAY6390 Posted October 14, 2009 Share Posted October 14, 2009 Well if it was the same as your one, it would need to have the ^ removed as you say, and that leaves it open to match again somewhere in the text if it's just part of the subject. With the ^ and the $ it's forcing a full line to be matched only Quote Link to comment https://forums.phpfreaks.com/topic/177571-find-text-in-data-and-then-read-until-new-line/#findComment-936671 Share on other sites More sharing options...
nrg_alpha Posted October 14, 2009 Share Posted October 14, 2009 Well if it was the same as your one, it would need to have the ^ removed as you say, and that leaves it open to match again somewhere in the text if it's just part of the subject. With the ^ and the $ it's forcing a full line to be matched only Jay, you don't need the $, nor make the quantifier lazy (but you are right about the 'm' modifier though, as CV's version of ^ would look for 'Search' at the beginning of the entire string). Reason being that since the OP only wants to match to a newline, and since the dot by default doesn't include this, that is why CV used .* to this advantage. My approach would be a hybrid of yours and CV's, with a dash of \K: $content = <<<EOF Tue, 13 Oct 2009 10:52:37 -0700 (PDT) Date: Tue, 13 Oct 2009 13:52:37 -0400 Message-ID: Subject: Test Subject From: Test User EOF; preg_match('#^Subject: \K.*#m',$content,$match); echo $match[0]; Quote Link to comment https://forums.phpfreaks.com/topic/177571-find-text-in-data-and-then-read-until-new-line/#findComment-936798 Share on other sites More sharing options...
JAY6390 Posted October 14, 2009 Share Posted October 14, 2009 What exactly does the \k do? Quote Link to comment https://forums.phpfreaks.com/topic/177571-find-text-in-data-and-then-read-until-new-line/#findComment-936800 Share on other sites More sharing options...
nrg_alpha Posted October 14, 2009 Share Posted October 14, 2009 What exactly does the \k do? You mean \K (cap letter) You can read up about that here. On a side note related to that article.. Member Salathe has pointed out to me that this (\K) is not an assertion, but rather an escape sequence.. so I'll have to amend that article to reflect this. EDIT - In case you were wondering why the use of \K in this example... it saves on using a capture. Quote Link to comment https://forums.phpfreaks.com/topic/177571-find-text-in-data-and-then-read-until-new-line/#findComment-936803 Share on other sites More sharing options...
JAY6390 Posted October 14, 2009 Share Posted October 14, 2009 Ah I see, thanks for the article link Quote Link to comment https://forums.phpfreaks.com/topic/177571-find-text-in-data-and-then-read-until-new-line/#findComment-936806 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.