正则表达式 - PHP如何获取HTML一个元素里面的内容
为情所困
为情所困 2017-05-16 13:06:41
0
3
503

目前我是通过从网上抄来的正则实现的,可是并不能达到我想要效果。

目前我的方案是:

 $text=file_get_contents('404.html');
 preg_match('/<time[^>]*itemprop=\"datePublished\".*?>.*?<\/time>/ism',$text,$match); 
print($match[0]); 

可是最终输出的内容是

<time datetime="2017-02-20T18:41:00+08:00" itemprop="datePublished">2017年2月20日</time>

我想要的是输出2017年2月20日,也就是原因里面的内容,可是对正则不了解,看了百科完全一脸懵逼。请问该如何实现?或是正则该怎么写才能输出里面的内容

为情所困
为情所困

reply all(3)
仅有的幸福

This time is similar to html tags. You can use PHP's html tag removal function to remove the nested tags and it will be fine. Take a look at the code below and try using the tag removal function strip_tags(). For more specific usage, you can check the manual.

$text=file_get_contents('404.html');
 preg_match('/<time[^>]*itemprop=\"datePublished\".*?>.*?<\/time>/ism',$text,$match); 
print(strip_tags($match[0])); 
阿神

1. https://github.com/bupt1987/h...
2. https://github.com/paquettg/p...

We directly recommend two php to parse html, similar to jQuery. Users can read html elements

PHPzhong

strip_tags is a function of php, used to remove html tags from strings, so you can use strip_tags here. Since you use regular expressions, you can also use regular expressions directly to optimize the program. Please see the rules below

preg_match('/<time[^>]*itemprop=\"datePublished\".*?>(.*?)<\/time>/ism',$text,$match);
print_r($match); 
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!