简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM