dadamssg Posted July 10, 2012 Share Posted July 10, 2012 I have this function to search for a 'word'(string with white spaces around it) after a specified string and it works great. I need to modify it to where it looks for all characters after the specified string until it hits a new line. The following returns "07/23/2012". But i would like to do is modify it so if i input "Property: " into the preg_match function it returns "The best resort in town". I suck at regex though. Any help would be much appreciated. thanks! $body = "Dear John Doe, your information is below Property: The best resort in town Check in date: 07/23/2012 Check out date: 07/26/2012 Alternate Check in date: 07/27/2012 Alternate Check out date: 07/29/2012"; if(preg_match('/(?<=Check in date: )\S+/i', $body, $match)) { echo $match[0]; } Link to comment https://forums.phpfreaks.com/topic/265483-get-text-after-certain-string-until-a-new-line/ Share on other sites More sharing options...
xyph Posted July 10, 2012 Share Posted July 10, 2012 #property: [^\r\n]+#i Options: case insensitive Match the characters ?property: ? literally ?property: ? Match the regular expression below and capture its match into backreference number 1 ?([^\r\n]+)? Match a single character NOT present in the list below ?[^\r\n]+? Between one and unlimited times, as many times as possible, giving back as needed (greedy) ?+? A carriage return character ?\r? A line feed character ?\n? Link to comment https://forums.phpfreaks.com/topic/265483-get-text-after-certain-string-until-a-new-line/#findComment-1360586 Share on other sites More sharing options...
dadamssg Posted July 10, 2012 Author Share Posted July 10, 2012 thanks for the that, xyph. I did the following but it is returning "Property: " with it. Anyway to cut that out? $body = "Dear John Doe, your information is below Check in date: 07/23/2012 Check out date: 07/26/2012 Property: The best inn around \n Alternate Check in date: 07/27/2012 Alternate Check out date: 07/29/2012"; echo "$body<br/><br/>"; if(preg_match('/(?<=Check in date: )\S+/i', $body, $match)) { echo $match[0]."<br/><br/>"; //returns '07/23/2012' } if(preg_match('#Property: [^\r\n]+#i', $body, $match)) { echo $match[0]; //returns 'Property: The best inn around' } Link to comment https://forums.phpfreaks.com/topic/265483-get-text-after-certain-string-until-a-new-line/#findComment-1360587 Share on other sites More sharing options...
xyph Posted July 10, 2012 Share Posted July 10, 2012 Ooops, for some reason I didn't paste the right RegEx, but the explanation has the change #property: ([^\r\n]+)#i and use var_dump( $match ); to find out how to get rid of property: Link to comment https://forums.phpfreaks.com/topic/265483-get-text-after-certain-string-until-a-new-line/#findComment-1360599 Share on other sites More sharing options...
dadamssg Posted July 10, 2012 Author Share Posted July 10, 2012 awesome. thanks. Here's my function function get_phrase_after_string($haystack,$needle) { //length of needle $len = strlen($needle); //matches $needle until hits a \n or \r if(preg_match("#$needle([^\r\n]+)#i", $haystack, $match)) { //length of matched text $rsp = strlen($match[0]); //determine what to remove $back = $rsp - $len; return trim(substr($match[0],- $back)); } } Link to comment https://forums.phpfreaks.com/topic/265483-get-text-after-certain-string-until-a-new-line/#findComment-1360607 Share on other sites More sharing options...
xyph Posted July 10, 2012 Share Posted July 10, 2012 You may want to make sure $needle doesn't contain special PREG characters. preg_quote Link to comment https://forums.phpfreaks.com/topic/265483-get-text-after-certain-string-until-a-new-line/#findComment-1360609 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.