I think with ER, if at all possible, would be more complicated or less readable.
But it is easy, assuming that this HTML is in a string, and as an alternative to @abfurlan's solution in case tags should be kept, just break it, separating them with new lines (not tags), iterating and comparing with the index:
$str = '<td>Conteúdo #1</td>
<td>Conteúdo #2</td>
<td>Conteúdo #3</td>
<td>Conteúdo #4</td>
<td>Conteúdo #5</td>
<td>Conteúdo #6</td>';
$tds = explode( "\n", $str );
$slice = array();
array_walk(
$tds,
function( $entry, $offset ) use( &$slice ) {
if( in_array( $offset, array( 1, 2, 5 ) ) ) $slice[] = $entry;
}
);
I preferred array_walk () , but you can do with a simple foreach
, too:
foreach( $tds as $offset => $entry ) {
if( in_array( $offset, array( 1, 2, 5 ) ) ) $slice[] = $entry;
}
As for having other elements before and after, then the thing changes a bit because you have to get in the string that you originally posted and did not say that it was in the middle of more HTML.
For this you have two options:
-
Syntactically analyze HTML, with DOM or SimpleXML a> (which is easier)
$xml = simplexml_load_string( $str );
$tds = $xml -> xpath( '//td' );
$slice = array();
foreach( $tds as $offset => $entry ) {
if( in_array( $offset, array( 1, 2, 5 ) ) ) $slice[] = (string) $entry;
}
To the detriment of losing the tags.
-
Use a Regular Expression to find <td>
:
$tds = preg_split( '/.*(<td>.*?<\/td>).*/', $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
The problem with this approach is that you get a little bit of junk that you need to clean up for offsets to match:
$tds = array_values( array_filter( array_map( 'trim', $tds ) ) );
array_map () with will apply trim () in all indexes, array_filter () will remove the empty entries and array_values () will reindex.
And the iteration remains the same.