php用正则如何去掉以下style标签里面的空格和换行

发布网友

我来回答

2个回答

热心网友

<?php

$str='<style>*
        {
        margin:0;
        padding:0;
        border:0;
        }

body
        {
        font-family:Verdana, Arial, "宋体";
        font-size:12px;
        }

strong
{
        font-family:"微软雅黑";
        font-size:14px;
        color:#000;
        font-weight:800;
}</style><html>xxxxxx</html><script>var xxxxxx=0;</script>';

$str = preg_replace_callback('/(<style>.*?<\/style>)/is', function($matched){
    return preg_replace('/[\s\n]+/is', '', $matched[1]);
}, $str);

echo $str;
exit;

热心网友

<?php
$str='<style>1x     xxxxx

abc</style><html>xxxxxx</html><script>xxxxxx</script>';
$matches=array();
preg_match('/<style>([^<]*)<\/style>/i',$str,$matches);
if(isset($matches[1])){
$s=$matches[1];
$s=str_replace(" ","",$s);
$s=str_replace("\n","",$s);
$s=str_replace("\r","",$s);
echo $s;

?>

追问我不是要把内容提取出来再替换哦,我要保留整个页面的字符串,但是里面的去掉空格和换行,你这样是把内容提取出来了,

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com