Udah baca dokumentasi PHP tetapi masih bingung cara menggunakan preg_match_all
? Coba ikuti tutorial dibawah ini, sekalian ikuti contohnya.
Contoh kita akan mengambil data dari file html tabel.html
<table cellpadding="1" cellspacing="1"> <tr> <th>name</th> <th>email</th> <th>address</th> <th>country</th> </tr> <tr> <td>Vivien Buckner</td> <td>magna.Duis.dignissim@tellussemmollis.edu</td> <td>7575 Pharetra Rd.</td> <td>Eritrea</td> </tr> <tr> <td>Derek Sharpe</td> <td>condimentum@euturpis.com</td> <td>7545 Lobortis. Road</td> <td>Bangladesh</td> </tr> <tr> <td>Bradley Dalton</td> <td>amet@posuere.com</td> <td>Ap #544-8283 Malesuada Av.</td> <td>Palestine, State of</td> </tr> <tr> <td>Gray Weber</td> <td>Donec@AeneanmassaInteger.org</td> <td>P.O. Box 555, 6707 Libero St.</td> <td>Ukraine</td> </tr> <tr> <td>Amery Petty</td> <td>Mauris.nulla@quispede.net</td> <td>P.O. Box 710, 9013 Et Avenue</td> <td>Lebanon</td> </tr> <tr> <td>Rahim Macias</td> <td>Nulla@ultriciesornareelit.ca</td> <td>849-820 Vivamus Street</td> <td>El Salvador</td> </tr> <tr> <td>Branden Farrell</td> <td>non@euismod.com</td> <td>P.O. Box 655, 3941 Pharetra. Road</td> <td>Comoros</td> </tr> <tr> <td>Ignatius Gonzales</td> <td>massa.Suspendisse@cubiliaCuraePhasellus.co.uk</td> <td>P.O. Box 782, 3264 Sed Ave</td> <td>Grenada</td> </tr> <tr> <td>Guy Singleton</td> <td>volutpat@arcuVivamus.org</td> <td>P.O. Box 130, 4058 Consequat Av.</td> <td>Nigeria</td> </tr> <tr> <td>Kaden Morgan</td> <td>Nulla@arcuVestibulum.com</td> <td>6753 Cras Av.</td> <td>Libya</td> </tr> <tr> <td>Brynne Mclean</td> <td>diam.Sed@molestiesodales.ca</td> <td>7243 Nunc Road</td> <td>Saint Martin</td> </tr> <tr> <td>Hedy Maxwell</td> <td>montes@Vivamusnonlorem.co.uk</td> <td>6038 Neque. Street</td> <td>Monaco</td> </tr> <tr> <td>Sonia Herman</td> <td>tincidunt@lorem.org</td> <td>4636 Mauris St.</td> <td>Martinique</td> </tr> <tr> <td>Jorden Luna</td> <td>facilisis.facilisis@disparturient.co.uk</td> <td>Ap #597-2554 Consequat, Rd.</td> <td>Pakistan</td> </tr> <tr> <td>Hyatt Williams</td> <td>molestie.dapibus.ligula@dictum.ca</td> <td>Ap #970-1226 Aliquet Av.</td> <td>Slovenia</td> </tr> <tr> <td>Fulton Walters</td> <td>facilisis@egetlaoreetposuere.co.uk</td> <td>8031 Semper Road</td> <td>Venezuela</td> </tr> </table>
Untuk mengambil semua data diantara tag < tr>
dan < /tr>
, gunakan script dibawah ini parsing-tabel.php
<?php // parsing-tabel.php $fileHTML = file_get_contents('/tmp/tabel.html'); // ambil semua data diantara <tr> dan </tr> preg_match_all("'<tr>(.*?)</tr>'si",$fileHTML, $ketemu); // cek array data // var_dump($ketemu); // $ketemu[0] atau array pertama masih memiliki tag <tr> </tr> // makanya kita memilih $ketemu[1] dari hasil var_dump($ketemu) diatas. foreach ($ketemu[1] as $data) { echo $data; }
kegunaan tiap baris dari script diatas bisa dilihat di script diatas.
Output script diatas :
<th>name</th> <th>email</th> <th>address</th> <th>country</th> <td>Vivien Buckner</td> <td>magna.Duis.dignissim@tellussemmollis.edu</td> <td>7575 Pharetra Rd.</td> <td>Eritrea</td> <td>Derek Sharpe</td> <td>condimentum@euturpis.com</td> <td>7545 Lobortis. Road</td> <td>Bangladesh</td> <td>Bradley Dalton</td> <td>amet@posuere.com</td> <td>Ap #544-8283 Malesuada Av.</td> <td>Palestine, State of</td> ... ...
preg_match_all(“‘(.*?)’si”,$fileHTML, $ketemu);
Tujuan untuk ‘si” itu apa kak ?