我有一个简单的表单,您可以在其中上传html文件,其结构如下所示:
<?xml version='1.0' encoding='utf-8'?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="he" xml:lang="he" dir="rtl">
<head>
<title>test</title>
<link rel="stylesheet" href="../Styles/cssreset-min.css" type="text/css"/>
<link rel="stylesheet" href="../Styles/page_styles.css" type="text/css"/>
<link rel="stylesheet" href="../Styles/style.css" type="text/css"/>
</head>
<body id="start">
<img src="../Images/cover.jpg" class="images" alt="Image 1"/>
</body>
</html>
我对编辑HTML文件的服务器端编码感兴趣,如何加载它,找到img标签并将其替换为<svg>
标签(例如),然后使用相同的名称和扩展名保存html文件。我已经尝试了很多次,但是我似乎对那里的所有不同方法感到困惑。该解决方案需要跨浏览器兼容,并且在可能的情况下不要使用任何依赖关系,例如,我尝试使用RecursiveTreeIterator一次,而我的托管公司阻止了它的运行。
谢谢
一般来说:
要从本地加载任何文件,然后处理内容并保存所需的结果
1-将文件加载到服务器端
2-处理内容
3-将结果保存在服务器端,并为客户端提供下载链接
因此您可以创建一个新文件(pars.php)并将此代码放入其中,因为您看到它具有注释以向您解释每个命令
php代码只是一个简单的示例,您可以对其进行改进
<html>
<head>
</head>
<body>
<form class="public" action="<?php echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>" method="POST" enctype="multipart/form-data">
<p><label for="file">File Name:</label> <input type="file" name="file" id="file" /></p>
<br/>
<p><input type="submit" value="Parsing" name="sendData"/></p>
</form>
</body>
</html>
<?php
//test if the file is selected
if(!isset($_POST['sendData'])) exit;
//get file name
$filename = $_FILES["file"]["name"];
//be sure this file is html file
$ext = explode('.', $filename);
if($ext[1] != 'html'){
echo '<br>bad file type, it must be html file';
exit;
}
//upload the file to temp area
move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);
//read the file
$file = @fopen($filename, 'r');
if($file === false){
echo 'Error when reading the file';
exit;
}
//reading line by line
$output ='';
while (($line = fgets($file)) !== FALSE) {
$output .= str_replace('<img', '<svg', $line);
}
fclose($file);
//write the new file
$result = file_put_contents($filename, $output);
echo $result;
if(!$result){
echo 'faild';
}else{
echo 'success <br/>';
$load_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$load_link = str_replace('pars.php', '', $load_link);
$load_link .= $filename;
echo '<a href='.$load_link.'>See the results</a><br>';
}
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句