Last active
May 10, 2017 23:44
-
-
Save viniciuscenci/bb0c4dc7b63a38a3904c16817fac3c82 to your computer and use it in GitHub Desktop.
PHP parse HTML table -> Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$html= 'oluna da esquerda, usando o botão "Adicionar mídia" (tamanho Médio).</li> | |
<li>Digite a descrição na coluna da direita.</li> | |
</ol> | |
<table width="500"> | |
<tbody> | |
<tr> | |
<td valign="top" width="200"> <img class="alignnone size-medium wp-image-38" src="http://localhost/adriano/wp-content/uploads/2017/05/SAM_0187-300x225.jpg" alt="" width="300" height="225" /></td> | |
<td valign="top">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td> | |
</tr> | |
<tr> | |
<td valign="top" width="200"> <img class="alignnone size-medium wp-image-38" src="http://localhost/adriano/wp-content/uploads/2017/05/SAM_0187-300x225.jpg" alt="" width="300" height="225" /></td> | |
<td valign="top">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td> | |
</tr> | |
<tr> | |
<td valign="top" width="200"> <img class="alignnone size-medium wp-image-38" src="http://localhost/adriano/wp-content/uploads/2017/05/SAM_0187-300x225.jpg" alt="" width="300" height="225" /></td> | |
<td valign="top">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</td> | |
</tr> | |
<tr> | |
<td valign="top" width="200"></td> | |
<td valign="top"></td> | |
</tr> | |
<tr> | |
<td valign="top" width="200"></td> | |
<td valign="top"></td> | |
</tr> | |
<tr> | |
<td valign="top" width="200"></td> | |
<td valign="top"></td> | |
</tr> | |
<tr> | |
<td valign="top" width="200"></td> | |
<td valign="top"></td> | |
</tr> | |
<tr> | |
<td valign="top" width="200"></td> | |
<td valign="top"></td> | |
</tr> | |
<tr> | |
<td valign="top" width="200"></td> | |
<td valign="top"></td> | |
</tr> | |
<tr> | |
<td valign="top" width="200"></td> | |
<td valign="top"></td> | |
</tr> | |
<tr> | |
<td valign="top" width="200"></td> | |
<td valign="top"></td> | |
</tr> | |
<tr> | |
<td valign="top" width="200"></td> | |
<td valign="top"></td> | |
</tr> | |
</tbody> | |
</table>'; | |
$dom = new DomDocument(); | |
$dom->loadHTML( $html ); | |
$xpath = new DOMXPath( $dom ); | |
$content = array(); | |
echo '<pre>'; | |
foreach($xpath->query('//table//tr') as $tr) | |
{ | |
$td_pair = array(); | |
foreach ($tr->childNodes as $td) | |
{ | |
$img = $xpath->query('./img', $td)->item(0); //get direct first child | |
print_r($img->getAttribute('src')); // get the src | |
//$td_pair[] = $td->nodeValue; | |
$content[] = $td->nodeValue; | |
} | |
//$content[] = $td_pair; | |
} | |
echo '</pre>'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment