Duc Doan
Duc Doan

Reputation: 415

How to get values between html tags in a string

I have a html string like this

<span class="date booking-date">Dec 2, 2015</span><span class="time booking-time">6:36 am</span>

I want to get 2 values Dec 2, 2015 and 6:36 (not include am) into 2 variables with PHP, should I use preg_match to do that?

Upvotes: 0

Views: 86

Answers (1)

Mihai Matei
Mihai Matei

Reputation: 24276

Instead of getting separate values using preg_match, you can use strip_tags and DateTime object like:

$string = '<span class="date booking-date">Dec 2, 2015</span><span class="time booking-time">6:36 am</span>';
$dateTime = new DateTime(strip_tags($string));

var_dump($dateTime->format('Y-m-d H:i:s'));

The output would be

string(19) "2015-12-02 06:36:00"

The advantage of doing like this is that you can manipulate the date however you want.

Upvotes: 1

Related Questions