如何保留HTML内容?

How to preserve HTML content?

提问人:Abhinav 提问时间:5/8/2021 最后编辑:Abhinav 更新时间:5/9/2021 访问量:80

问:

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

下面是 html 内容的片段。内容-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>

观察到 =0A 被添加到 Content-1 中,每个换行符都用“=”添加,3D 也被添加到文件中。

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

php xml html 解析

评论


答:

1赞 Kevin Gales 5/9/2021 #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); 

?>  

评论

1赞 Abhinav 5/9/2021
@kelvin Gales,非常感谢你,你的解决方案解决了我的问题,再次感谢。