删除 php 动态整数中的部分字符串

remove part of string in php dynamic integer

已解决

$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

请注意 $replace 没有 data-code="" 值,因为它在 $str 中是动态的

如何删除这些号码?

抱歉我的英语不好

感谢大家的帮助^^ 问题已解决!

一种方法是使用 DOMDocumentDOMXPath

$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;

你好如果你想 delete 包含来自 database 的值的 td 你应该试试这个

假设数据库中的值为

$data = 19216;// from database
data-code="'.$data.'" // add this in $replace 

然后是你的代码

$data = 19216;
$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="'.$data.'">&times;</a></td>';// add $data here 
print_r (str_ireplace($replace, " ", $str));

这将删除包含 data-code ="19216";

的整个 td