Reputation: 1863
I have following text after reading a file:
06/16/2011 04:01:05 AM : Process Start
06/16/2011 04:01:05 AM : Creating File
06/16/2011 04:01:05 AM : Opening File
06/16/2011 04:01:05 AM : Writing Text
06/16/2011 04:01:05 AM : Closing File
06/16/2011 04:01:05 AM : Sending File in email
06/16/2011 04:01:05 AM : Process End
I want to remove date and time in the beginning of each line like this:
Process Start
Creating File
Opening File
Writing Text
Closing File
Sending File in email
Process End
How can we do this with string matching technique ?
Thanks
Upvotes: 2
Views: 220
Reputation: 1182
$str = "text : to : remove : text to keep";
$pos = strrpos($str, ":");
$str2 = substr($str, $pos+1);
echo $str2;
edit: changed strpos to strrpos, to find the last occurrence, as per comment below
Upvotes: 1
Reputation: 9121
One way to do this would be the following: (assuming the prefix is always the same length)
$output = '';
$tmp = explode("\n",$input);
foreach($tmp as $t)
$output .= substr($t,25)."\n";
echo $output;
First you explode
the input into a variable by splitting it on every new line, and then for each line you remove the first 25 characters to create a new output.
Upvotes: 5
Reputation: 9572
According to your example it seems that the best thing is to use substr()
because the size of the prefix is always the same. But if you really need a matching technique it will look something like this:
$new_line = preg_replace('^/\d{2}\/\d{2}\/\d{4}\/ \d{2}:\d{2}:\d{2}\/ (?:AM|PM) : /', '', $line);
Upvotes: 2
Reputation: 7188
If all strings are prefixed with text of the same length, use substring. If not, use regular expression matching.
Upvotes: 2
Reputation: 767
Date and time have constant length in your file. Use substr function to cut off 25 symbols from start of each line.
Upvotes: 2