任意一个页面,获取它<body>...</body>之间的内容,用一下函数
我们应该用如下函数
function get_tag_data($str, $start, $end){
if ( $start == '' || $end == '' ){
return;
}
$str = explode($start, $str);
$str = explode($end, $str[1]);
return $str[0];
}然后直接$getcontent = get_tag_data($fcontents, "<body>","</body>");但这里会出现这么一个问题,就是当<body>有属性的时候,问题就出现了,比如<body id="nobody">...</body>,<body id="allframe">...</body>等等..
这个时候上面的就不能获得,他们之间的内容,除非为这些带有属性的单独写内容,请问有什么办法,通过一个正值的方式,不管<body>内是否有属性,都能获取他们之间的内容呢?谢谢
我们应该用如下函数
function get_tag_data($str, $start, $end){
if ( $start == '' || $end == '' ){
return;
}
$str = explode($start, $str);
$str = explode($end, $str[1]);
return $str[0];
}然后直接$getcontent = get_tag_data($fcontents, "<body>","</body>");但这里会出现这么一个问题,就是当<body>有属性的时候,问题就出现了,比如<body id="nobody">...</body>,<body id="allframe">...</body>等等..
这个时候上面的就不能获得,他们之间的内容,除非为这些带有属性的单独写内容,请问有什么办法,通过一个正值的方式,不管<body>内是否有属性,都能获取他们之间的内容呢?谢谢
preg_match_all('/<body([^>]*)>([\d\D]*)<\/body>/iU',$str,$match);
$str = '<body>第一个body 内的内容</body>432432432<body>第二个body 内的内容</body>';
preg_match_all("/<body([^>]*)>([\d\D]*)<\/body>/iU",$str,$match);
print_r($match);
preg_match_all('/<body[^>]*>(.*)<\/body>/i',$str,$match);
var_dump($match[1]);
真好,用这个代码讲解一下 使用 U修正符和不使用的差别.首选对其代码中的小错进行修正。preg_match_all('/<body[^>]*>([\d\D]*)<\/body>/i',$string,$match);var_dump($match[1]);
//注意 (.*)是无法匹配换行和制表符的,通常用[\d\D]来处理
然后再用3楼提供的代码运行一次。
<?php $str = '<body>the first body</body>432432432<body>the seconde body</body>';preg_match_all('/<body[^>]*>(.*)<\/body>/i',$str,$match);
var_dump($match[1]);
?>/***
结果是 红色应该不是需要的吧。
array(1) {
[0]=>
string(52) "the first body</body>432432432<body>the seconde body"
}
*/
使用的U修正符后
$str = '<body>the first body</body>432432432<body>the seconde body</body>';preg_match_all('/<body[^>]*>([\d\D]*)<\/body>/iU',$str,$match);
var_dump($match[1]);
/**
array(2) {
[0]=>
string(14) "the first body"
[1]=>
string(16) "the seconde body"
}
*/最后,建议楼主,自己多实践,才能更好的体会。试试就知道具体是怎么回事了。
嗯,解释得非常好。U 这个修饰词我也不怎么会,因为少用,刚试了一下,确实好用。还 有我原来的 正则,少了一个 转义的。 四楼己补充了。。至于要匹配所有的字符,我原来的是不行的。应该为 ([\d\D]*)或者 ([\s\S]*) ,其实可以看出,与斥的两个就行了
{
if ( $tag == '')
return 0;
else
{
preg_match_all('@<'.$tag.'[^>]*>([\d\D]*)</'.$tag.'>@iU',$str,$match);
return $match[1];
}
}$str = '<body>the first body</body>432432432<body>the seconde body</body>';
$getcontent = get_tag_data($str, 'body');
print_r($getcontent); // Array ( [0] => the first body [1] => the seconde body )
?>
在多个网页中,<body的属性是不规则的
有的是<body id="nobody">...</body>这样,加了id="nobody"属性的
有的是<body style="width:100%">...</body>这样加了属性的
还有的是<body>...</body>没有加属性的
而不是同一个页面中有多个BODY
我想用一个正值,能获取到页面中body之间的内容,不用单独为这些body一个一个写,不知道是否明白我的意思,谢
<?php
function get_tag_data($str, $tag)
{
return preg_match('#<'.$tag.'[^>]*>(.*)</'.$tag.'>#is',$str,$m) ? $m[1] : false;
}
$str = '<body>
hi,1111111111111
1111111111111111
</body>
';
var_dump(get_tag_data($str, 'body'));
$str = '<body id="nobody">
hi,2222222222222
2222222222222222
</body>
';
var_dump(get_tag_data($str, 'body'));
$str = '<body style="width:100%" id="nobody">
hi,3333333333333
3333333333333333
</body>
';
var_dump(get_tag_data($str, 'body'));
$str = '<div style="width:100%" id="nobody">
hi,3333333333333
3333333333333333
</div>
';
var_dump(get_tag_data($str, 'body'));
?>