Home >Backend Development >PHP Tutorial >求用正则或其他方法取出指定特征的字符串

求用正则或其他方法取出指定特征的字符串

WBOY
WBOYOriginal
2016-06-20 12:42:17916browse

有这样一个字符串:

$str = '<div class="info">                <small>时间:</small>2015-12-24 09:47<small>来源:</small>华尔街见闻 <small>作者:</small>member <small>点击:</small>               <script src="/plus/count.php?view=yes&aid=467&mid=2" type="text/javascript" language="javascript"></script>145   次           </div>';


需要取出其中的日期时间:2015-12-24 09:47,日期是变化的,长度不定。
想了半天不知道怎么取。。求帮助!谢谢


回复讨论(解决方案)

匹配 时间: 和来源 之间的吧。。

/d+-\d+-\d+ \d+:\d+/

/d+-\d+-\d+ \d+:\d+/


header('Content-type:text/html;charset=UTF-8');$str = '<div class="info">                <small>时间:</small>2015-12-24 09:47<small>来源:</small>华尔街见闻 <small>作者:</small>member <small>点击:</small>               <script src="/plus/count.php?view=yes&aid=467&mid=2" type="text/javascript" language="javascript"></script>145   次           </div>';preg_match_all('/d+-\d+-\d+ \d+:\d+/', $str, $matches);print_r($matches);

我觉得思路是对的,可是结果是空数组Array ( [0] => Array ( ) )

不好意思,漏了个  \

preg_match_all('/\d+-\d+-\d+ \d+:\d+/', $str, $matches); print_r($matches);
Array ( [0] => Array ( [0] => 2015-12-24 09:47 ) ) 

不好意思,漏了个  \

preg_match_all('/\d+-\d+-\d+ \d+:\d+/', $str, $matches); print_r($matches);
Array ( [0] => Array ( [0] => 2015-12-24 09:47 ) ) 


我也没注意到漏了个\。
。多谢了,热心的版主大人。。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn