2011-12-11 94 views
-1

可能重复:
Best way to parse HTML with PHP提取字符串

我是新来的PHP,并希望从中提取这种格式的字符串第二个日期:

<span class="date-display-start">28.12.2011</span><span class="date-display-separator"> - </span><span class="date-display-end">05.01.2012</span> 

我不确定我可以最好的使用什么PHP功能来做到这一点?

+0

http://php.net/manual/en/class.domxpath.php –

+0

打我吧:) –

回答

2

我会用一个简单的正则表达式。假设您不需要比这更高级的数据提取,那么使用类或类似的东西看起来过于夸张。

preg_match('/<span class="date-display-end">(.*)<\/span>/U', $str, $result); 
print array_pop($result); 
0

您可以在许多不同的方式获得此,其中一个是“/\d{2}\.\d{2}\.\d{4}/”正则表达式:

<?php 

$subject = '<span class="date-display-start">28.12.2011</span><span class="date-display-separator"> - </span><span class="date-display-end">05.01.2012</span>'; 

$regexp = '/\d{2}\.\d{2}\.\d{4}/'; 

preg_match_all($regexp, $subject, $matches); 

print $matches[0][1]; 

上面的代码将打印“05.01.2012”,为this demo