Yoeri Plovie Yoeri Plovie - 3 months ago 16
PHP Question

remove part of string in php dynamic integer

SOLVED

$str = '<td class="text-right"> <a href="#" class="remove-item" data-code="24433">&times;</a></td>';
$str .= '<td class="text-right"> <a href="#" class="remove-item" data-code="19216">&times;</a></td>';

$replace = '<td class="text-right"> <a href="#" class="remove-item" data-code="HOW CAN I REMOVE THE NUMBERS FROM ABOVE?">&times;</a></td>';

print_r (str_ireplace($replace, " ", $str));


see result when deleting the tag

note that the $replace has no data-code="" value because it's dynamic in $str

how can i delete those numbers?

sorry for my bad English

Thanks people for you're help ^^
problem solved!

Answer

One way you could do it is to use DOMDocument and DOMXPath

$str = '<td class="text-right"> <a href="#" class="remove-item" data-code="24433">&times;</a></td>
        <td class="text-right"> <a href="#" class="remove-item" data-code="19216">&times;</a></td>';

$dom=new DOMDocument;
$dom->loadHTML( $str );
$xp=new DOMXPath( $dom );
$col=$xp->query('//td/a');

if( $col ){
    foreach( $col as $n )$n->parentNode->removeChild( $n );
}

echo $dom->saveHTML();
$dom=$xp=null;