Bonjour à tous,
Je souhaite remplir une base de données en me basant sur une liste d'un site web, pour cela j'aimerais passer du html (mal foutu) de la page à un csv.
Chaque élément de la liste ressemble à ça
Et mon but c'est d'extraire cette partieCode:
1
2
3
4
5
6
7
8
9
10
11
12 <div id="results-panel-content" class="list"><div class="search-result-product fltleft"> <a href="URL" title="View Details" class="prod-thumb"> <img src="URL" alt="bla" height="100" class="large"/> <img src="URL" alt="bla" class="small"/></a> <span class="product-details"><span class="class-image"> <img src="URL" width="25" height="30" alt=" Exclusive"/></span> <span class="prod-name"><a href="URL" title="blabla">blabla</a></span> <span class="prod-type">blabla</span></span> <span class="status-banner">Status: <span class="in-stock">in-stock</span></span> <span class="prod-price"><span class="price normal">blabla</span></span> <span class="flex-available"><a href="URL" title="View option">blabla <strong>blabla</strong>/ month</a></span> <span class="prod-link"><a href="URL" title="bla"><img src="URL" width="130" height="20" alt="Learn More"/></a></span></div>
Pour cela 2 regexCode:
1
2
3 <span class="prod-name"><a href="URL" title="bla">bla</a></span> <span class="prod-type">bla</span></span>
Code:#^<span class="prod-name"><a href=.*</span>$#i
Et mon codeCode:#^<span class="prod-type">.*</span>$#i
Au final j'obtiens bien tri.txt mais il est totalement vide, et j'arrive pas à voir ou se trouve mon erreur :cry:Code:
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 <?php $pattern1="#^<span class=\"prod-name\"><a href=.*</span>$#i"; $pattern2="#^<span class=\"prod-type\">.*</span>$#i"; //on ouvre le fichier dans lequel on veut lire $file = fopen('listing.html', 'r+'); //on ouvre le fichier dans lequel on veut écrire $file2 = fopen('tri.txt', 'a+'); if ($file){ /*Tant que l'on est pas à la fin du fichier*/ while (!feof($file)) { /*On lit la ligne courante*/ $buffer = fgets($file); /*si cela correspond à la regex on écrit dans tri.txt*/ if(preg_match_all($pattern1, $buffer, $matches)){ fputs($file2, $matches[0][0]); } }} fclose ($file); fclose ($file2); echo"done" ?>