Link to home
Create AccountLog in
Avatar of rgb192
rgb192Flag for United States of America

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|spacedbqueryspace|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




Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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.
Avatar of rgb192

ASKER

I dont understand.  I am not good with regular expressions
Avatar of stalhw
stalhw

$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.
hmm correction to the preg_match:

if (preg_match("/^([0-9\/]{10} [0-9:]{8}[AP]M)\s*\|([^\|]*)\|\s\|([^\|]*)/",$line,$matched)) {
Avatar of rgb192

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);

}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of stalhw
stalhw

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of rgb192

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
<?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);

Open in new window