Je suis chargé d'extraire des données du HTML où j'ai besoin d'obtenir des tableaux de données pour chaque ensemble de p balises dans le HTML. Voici un exemple de code HTML.
$title_label = 'Title';
$number_label = '1234';
preg_match_all('%\d{2}px; white-space: nowrap;">$title_label </p>%', $html_content, $array_match);
$array_cost_name = $array_match[1];
$array_return_name = $array_match[2];
$array_number_name = $array_match[3];
Ce code HTML se répète plusieurs fois, les libellés "Titre" et "1234" restent les mêmes, puis passe à des libellés différents à un certain moment point. Les valeurs «haut» et «gauche» changeront constamment dans tout le HTML. J'ai la possibilité de parcourir les étiquettes existantes «Titre» et «1234» pour correspondre à cette partie des choses.
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 63px; white-space: nowrap;">Title </p> <p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 349px; white-space: nowrap;">1234 </p> <p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 461px; white-space: nowrap;">$30 </p> <p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 563px; white-space: nowrap;">$10,000,000 </p> <p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 777px; white-space: nowrap;">3,000,000 </p>
J'aurais alors besoin des 3 tableaux pour contenir les 3 derniers champs d'étiquette. Dans le cas de l'exemple HTML fourni, je voudrais que "30 $", "10 000 000 $" et "3 000 000" soient les premières valeurs de chaque tableau.
Je ne sais pas comment écrire une expression régulière pour gérer cette situation. Quelqu'un peut-il aider?
3 Réponses :
Les expressions régulières ne sont pas le bon outil pour cette tâche, un analyseur XML est beaucoup plus simple:
$html = '<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 63px; white-space: nowrap;">Title </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 349px; white-space: nowrap;">1234 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 461px; white-space: nowrap;">$30 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 563px; white-space: nowrap;">$10,000,000 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 777px; white-space: nowrap;">3,000,000 </p>';
$doc = new DOMDocument();
$doc->loadHTML($html);
$xml = simplexml_import_dom($doc);
$parts = $xml->xpath('//p[@class="ft01"]/text()'); // find all texts inside p tags, with class ft01
$array_cost_name = (string) $parts[2];
$array_return_name = (string) $parts[3];
$array_number_name = (string) $parts[4];
echo $array_cost_name ; // $30
echo $array_return_name ; // $10,000,000
echo $array_number_name ; // 3,000,000
Merci, cela fonctionne parfaitement pour moi.
Vous pouvez utiliser une simple expression régulière globale / ace: nowrap; "> (. *) / ou tout autre élément le long de la ligne pour obtenir le groupe que vous recherchez, puis en supprimant les deux premiers éléments pour n’obtenir que les trois derniers. Voici un exemple et un lien vers testez-le.
$html_content = '<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 63px; white-space: nowrap;">Title </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 349px; white-space: nowrap;">1234 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 461px; white-space: nowrap;">$30 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 563px; white-space: nowrap;">$10,000,000 </p>
<p class="ft01" style="margin: 0; padding: 0; font-size: 16px; font-family: Times; color: #000000; position: absolute; top: 103px; left: 777px; white-space: nowrap;">3,000,000 </p>';
preg_match_all('/ace: nowrap;">(.*) <\/p>/', $html_content, $array_match);
$array_match = array_slice($array_match[0], 2); ;
print_r($array_match);
http://sandbox.onlinephpfunctions.com/ code / 5ac69d44ff8168b4b21133c46dfa9c6db6986b6a
via regex, vous pouvez essayer de cette façon: Ceci capturera tous les caractères entre p> code> étiquette. p> p>
Pensez à utiliser un analyseur Dom avec php, ce sera plus efficace. php.net/manual/fr/domdocument.loadhtmlfile.php