Jeff Long
Jeff Long

Reputation: 69

Ignoring the rest of a string after a certain character in PHP

How would I ignore everything after a certain section in a string?

The challenge is that the string is variable and won't be the same every time.

Here's an example of what I want to do:


This is the part of the string I want to read

I DO NOT WANT TO READ ANYTHING FROM THIS POINT ON...

(IGNORE ALL OF THIS) On Sun, Jan 15, 2012 at 11:55 AM, wrote:

Then I want to return JUST the part I want to read so I can check it and do what I need with that data...The problem is that the "On Sun, Jan 15, 2012 at 11:55 AM, wrote:" is always going to be different and variable...so I think I would have to use a preg_match to find this phrase? Not sure on this...thanks for your help!

Just thought of something else I could do to make this work...

What I'm doing is checking the body of an email for certain words or phrases and then performing a specific task if a phrase is found...I already have that part written. What could POSSIBLY work is if a particular phrase is found in the string, I would need it to STOP checking the rest of the string. Crap...actually this will ONLY work if the phrase IS found in the first part...if it's NOT found in the first part, then the script would continue checking the string and it might find the phrase I am looking for in the second part which would cause my functionality to not perform correctly. Just a thought....

Upvotes: 0

Views: 1684

Answers (2)

Tom
Tom

Reputation: 3520

$string = "This SHOULD be displayed    On Tue, Jan 17, 2012 at 6:05 PM   I wrote: And this should NOT be displayed";
$output = array();

preg_match("/On (\w{3}), (\w{3}) (\d{2}), (\d{4}) at (\d{1,2}:\d{2}) (\w{2})/", $string, $output);
$string = explode($output[0], $string);
$string = trim($string[0]);

This will cut the string at On Tue, Jan 17, 2012 at 6:05 PM and return the text preceding the date.

Upvotes: 1

Related Questions