Jump to content

get text after certain string until a new line


dadamssg

Recommended Posts

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
Share on other sites

#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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.