【问题标题】:How to preserve HTML content?如何保存 HTML 内容?
【发布时间】:2021-07-30 13:09:14
【问题描述】:

试图保留由 powerMTA 在特定位置生成的 HTML 内容。

下面是html内容的sn-p。内容-1。

<html>=0A<body>=0A<table style=3D"max-width:576px;font-family:Arial, Helvet=
ica, sans-serif;" border=3D"0" width=3D"100%" cellspacing=3D"0" cellpadding=
=3D"0" align=3D"center" >=0A<tr>=0A<td style=3D"text-align:center;">=0A<a h=
ref=3D"https://www.anshdc.com/mkt/v1/?utm_source=3Daffiliate&utm_medium=3Dc=
pv&utm_campaign=3Ddjshdjdh.com&utm_content=3Dsms&utm_term=3D1"><img src=3D"=
https://s3.sdjjfncjsj.amazonaws.com/image.ahdhdhfifkfhfi.co.in/8362/8362.jp=
g">=0A</a>=0A</td>=0A</tr>=0A</table>=0A</body>=0A</html>=0A

需要保留的内容是:Content-2。

<html>
<body>
<table style="max-width:576px;font-family:Arial, Helvetica, sans-serif;" border="0" width="100%" cellspacing="0" cellpadding="0" align="center" >
<tr>
<td style="text-align:center;">
<a href="https://www.anshdc.com/mkt/v1/?utm_source=affiliate&utm_medium=cpv&utm_campaign=villa&utm_content=sms&utm_term=1"><img src="https://s3.sdjjfncjsj.amazonaws.com/image.ahdhdhfifkfhfi.co.in/8362/8362.jpg">
</a>
</td>
</tr>
</table>
</body>
</html>

观察到在Content-1中添加了=0A,并且每个换行符都添加了'=',并且文件中也添加了3D。

我们要解析 content-1 并使用 PHP 脚本将其保存为 content-2。

【问题讨论】:

    标签: php html xml-parsing html-parsing


    【解决方案1】:

    这是一种方法...

    <?php 
    
    $sub = file_get_contents("content-1.txt"); //avoided file() cause it wont work because the last line does not end with = it did not want to take the long route of adding it first
     
    $subb = str_replace(['=3D','=0A'],['=',''],$sub); //replace noticeable unwanted
    
    $content = "";
    $line = strtok($subb, "\r\n"); //break at end of every line even spaces are considered as chars here, !important for last line
    
    while ($line !== false) {
        $content .= substr($line,0,-1); // get content before last character on each line, where it does not exists space is considered last character
        $line = strtok("\r\n"); 
    }
    
    file_put_contents("content-2.txt",$content); 
    
    ?>  
    

    【讨论】:

    • @kelvin Gales,非常感谢,您的解决方案再次解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 2016-05-08
    相关资源
    最近更新 更多