2

I'm trying to grab part of a string that represents a date.

The date-string will usually, but not always, have regular text before and/or after it.

In this example:

Sometimes text is here, Sun, Apr 09, 2000  And sometimes but not always text here

I would want the result to be:

Sun, Apr 09, 2000

Bear in mind that days and month strings can be 3 or 4 characters in length.

My meager attempt is:

$test = "Sometimes text is here, Sun, Apr 09, 2000  And sometimes but not always text here";

if (ereg ("/([a-z]{3,4}),.([a-z]{3,4}).([0-9]{1,2}),.([0-9]{4})/i", $test, $regs)) {
    echo "$regs[4].$regs[3].$regs[2].$regs[1]";
}

Also interested in hearing non-regex based solutions.

3 Answers 3

2

Someone could probably do better than this as it's pretty verbose:

/(?:mon|tues?|weds|thurs?|fri|sat|sun), [a-z]{3,4} [0-9]{1,2}, [0-9]{4}/i

$regex = '/(?:mon|tues?|weds|thurs?|fri|sat|sun), [a-z]{3,4} [0-9]{1,2}, [0-9]{4}/i';
$string = 'Sometimes text is here, Sun, Apr 09, 2000  And sometimes but not always text here';

preg_match($regex, $string, $matches);

echo $matches[0];
// Sun, Apr 09, 2000

If you're expecting the date to occur multiple times, a minor change helps.

// store the match as a named parameter called 'date'
$regex = '/(?<date>(?:sun|mon|tues?|weds|thurs?|fri|sat|sun), [a-z]{3,4} [0-9]{1,2}, [0-9]{4})/i';

$string = 'Sometimes text is here, Sun, Apr 09, 2000  And sometimes but not always text here. Sun, Mar 10, 2010';

preg_match_all($regex, $string, $matches);

print_r($matches['date']);
/* 
 Array
    (
        [0] => Sun, Apr 09, 2000
        [1] => Sun, Mar 10, 2010
    )
*/

Started off with the name of the day, just on the off chance you get something that looks the same as a day but isn't.

I'd also not suggest using ereg() as it was deprecated in 5.3.0. Use preg_match() instead, or one of the other preg_* functions.

Sign up to request clarification or add additional context in comments.

Comments

1

This regex seems to work in multiple cases:

$str = "Sometimes text is here, Sun, Apr 09, 2000  And sometimes but not always text here";
$reg = '/(\w{3}),\s*(\w{3})\s*(\d+),\s*(\d{4})/';

$match = preg_match($reg, $str, $matches);

if ($match) {
    $date = "{$matches[2]} {$matches[3]} {$matches[4]}\n";
    // Apr 09 2000
    $timestamp = strtotime($date);
}

ereg() should no longer be used, as of PHP 5.3.0 it is deprecated and preg has long been favored as a faster, more widely used alternative.

Comments

1

Instead of relying on deprecated ereg, try preg_match_all.

$str = "Sometimes text is here, Sun, Apr 09, 2000  And sometimes but not always text here";

preg_match_all('/.*([A-Za-z]{3,4}, [A-Za-z]{3,4} [\d]{1,2}, [\d]{4}).*/',$str,$matches);

Output

(
    [0] => Array
        (
            [0] => Sometimes text is here, Sun, Apr 09, 2000  And sometimes but not always text here
        )

    [1] => Array
        (
            [0] => Sun, Apr 09, 2000
        )

)

You'll find all matches in $matches[1].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.