I think with ER, if possible, it would be more complicated or less readable.
But it’s easy, assuming that this HTML is in a string, and as an alternative to the @abfurlan solution in case the tags should be kept, just break it up, separating by the new lines (and not by the tags), iterate and compare 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 foreach
simple, 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 little because you have to get to the string you originally posted and didn’t say it was in the middle of more HTML.
For that you have two options:
Syntactically parse HTML, with GIFT or Simplexml (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 tags.
Use Regular Expression to locate <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 some garbage that you need to clean up so that the offsets coincide:
$tds = array_values( array_filter( array_map( 'trim', $tds ) ) );
array_map() how will you apply Trim() in all indexes, array_filter() will remove the empty entries and array_values() will reindexe.
And the iteration remains the same.
What is this
er
?– Maniero
Regular Expression, but optional
– Alisson Acioli
Tags that only you know what it means should not be used. Tags serve to classify content. I already thought it was
Entidade-Relacionamento
. It would make more sense to me. And what is optional you can even ask the question but not use a tag.– Maniero