0

I have a string like this:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean 2017-03-31 ligula eget dolor. Aenean massa. 2016-03-04 sociis natoque penatibus...

I would like to get all the dates inside the text... the format dates are always yyyy-mm-dd.

Any suggestions?

2

3 Answers 3

1

Try to use preg_match and regex expression:

$string = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean 2017-03-31 ligula eget dolor. Aenean massa. 2016-03-04 sociis natoque penatibus...";
$pattern = '/([0-9]{4}-[0-9]{2}-[0-9]{2})/';
preg_match_all($pattern, $string, $matches);
print_r($matches);

To test regex, I suggest you to use https://regex101.com/

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

Comments

1
preg_match("/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/", $input_line, $output_array);

Comments

1

Alright so what you have to do is figure out what you are trying to find, in your case yyyy-mm-dd.

So this would be it:

[0-9]{4}-[0-9]{2}-[0-9]{2}

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.