1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
//ouverture du fichier html
$hHtml = fopen("./fichier.html", "r+");
//ouverture du fichier csv
$hCsv = fopen("./fichier.csv", "w+");
//lecture du fichier html
$contents = array();
while(($str = fread ($hHtml, 8192))!=false){
array_push($contents, $str);
}
fclose ($hHtml);
//traitement de la lecture
$new_contents = array();
while(sizeof($contents)){
$str = array_shift($contents);
$str = preg_replace('`<th([^>]+)>`', '"', $str);
$str = preg_replace('`</th>`', '";', $str);
$str = preg_replace('`<td([^>]+)>`', '"', $str);
$str = preg_replace('`</td>`', '";', $str);
$str = preg_replace('`\s`', '', $str);
$str = preg_replace('`<html>|<body>|<tbody>|<tr>|</tbody>|</a>|<div([^>]+)>|<thead>|</thead>|<a([^>]+)>|<table([^>]+)>|<h([^>]+)>|</h([^>]+)>|<img([^>]+)>|<tr([^>]+)>`', '', $str);
$str = preg_replace('`</tr>`', '\\r\\n' , $str);
$str = preg_replace('/""+/', '', $str);
array_push($new_contents, $str);
}
//écriture du fichier csv
while(sizeof($new_contents)){
fwrite($hCsv, array_shift($new_contents));
}
fclose($hCsv); |
Partager