简体   繁体   English

如何在字符串中的html标记之间获取值

[英]How to get values between html tags in a string

I have a html string like this 我有一个这样的html字符串

<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? 我想使用PHP将2个值Dec 2, 20156:36 (不包括am ))转换为2个变量,我应该使用preg_match做到这一点吗?

Instead of getting separate values using preg_match , you can use strip_tags and DateTime object like: 可以使用strip_tagsDateTime对象,而不是使用preg_match获得单独的值:

$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. 这样做的好处是您可以根据需要操纵日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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