rgb192
asked on
want certain lines that match a pattern in a string
$string='
05/24/2011 04:55:42PM | dbquery | | words words
line2
line3
another line
06/27/2011 05:58:40PM | dbquery | | more words
';
I want to echo all the lines where the line begins with
timespace|spacedbqueryspac e|space|
05/24/2011 04:55:42PM | dbquery | |
so the answer is
05/24/2011 04:55:42PM | dbquery | | words words
06/27/2011 05:58:40PM | dbquery | | more words
05/24/2011 04:55:42PM | dbquery | | words words
line2
line3
another line
06/27/2011 05:58:40PM | dbquery | | more words
';
I want to echo all the lines where the line begins with
timespace|spacedbqueryspac
05/24/2011 04:55:42PM | dbquery | |
so the answer is
05/24/2011 04:55:42PM | dbquery | | words words
06/27/2011 05:58:40PM | dbquery | | more words
You can use explode(PHP_EOL) to isolate the lines, then use explode('|') to isolate the fields in each line. Then you can use strtotime() on position zero of the resulting array. If it returns anything other than FALSE, you've got one of the lines that begins with the DATETIME value.
ASKER
I dont understand. I am not good with regular expressions
$arrayS=preg_split("[\n|\r ]",$string );
foreach ($arrayS as $line) {
if (preg_match("/([0-9\/]{10} [0-9:]{8}[AP]M)\s*|([^|]*) |\s|([^|]* )/",$line, $matched)) {
print "\nLine: $line\nMatched: ";
print_r($matched);
}
}
something like this should do it.
foreach ($arrayS as $line) {
if (preg_match("/([0-9\/]{10}
print "\nLine: $line\nMatched: ";
print_r($matched);
}
}
something like this should do it.
hmm correction to the preg_match:
if (preg_match("/^([0-9\/]{10 } [0-9:]{8}[AP]M)\s*\|([^\|] *)\|\s\|([ ^\|]*)/",$ line,$matc hed)) {
if (preg_match("/^([0-9\/]{10
ASKER
there was no output
<?php
$string='
05/24/2011 04:55:42PM | dbquery | | words words
line2
line3
another line
06/27/2011 05:58:40PM | dbquery | | more words
';
$arrayS=preg_split("[\n|\r]",$string);
foreach ($arrayS as $line) {
//if (preg_match("/([0-9\/]{10} [0-9:]{8}[AP]M)\s*|([^|]*)|\s|([^|]*)/",$line,$matched)) {
if (preg_match("/^([0-9\/]{10} [0-9:]{8}[AP]M)\s*\|([^\|]*)\|\s\|([^\|]*)/",$line,$matched)) {
print "\nLine: $line\nMatched: ";
print_r($matched);
echo "\nLine: $line\nMatched: ";
echo($matched);
}
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
thanks
No regular expressions are needed. I'll post a code sample in a moment.
http://www.laprbass.com/RAY_temp_rgb192.php
Outputs:
05/24/2011 04:55:42PM | dbquery | | words words
06/27/2011 05:58:40PM | dbquery | | more words
Outputs:
05/24/2011 04:55:42PM | dbquery | | words words
06/27/2011 05:58:40PM | dbquery | | more words
<?php // RAY_temp_jd11b.php
error_reporting(E_ALL);
date_default_timezone_set('America/Chicago');
// TEST DATA FROM THE POST AT EE
$string='
05/24/2011 04:55:42PM | dbquery | | words words
line2
line3
another line
06/27/2011 05:58:40PM | dbquery | | more words
';
// SEPARATE THE LINES
$arr = explode(PHP_EOL, $string);
$out = array();
foreach ($arr as $line)
{
// SEPARATE THE FIELDS IN THE LINE
$new = explode('|', $line);
// IF NOT A DATETIME, SKIP IT
if (strtotime($new[0]) === FALSE) continue;
$out[] = $line;
}
// RECONSTRUC THE STRING
$answer = implode(PHP_EOL, $out);
// SHOW THE WORK PRODUCT
echo "<pre>";
print_r($answer);