preg_replace_callback( "/[0-9]*/", array( &$this, '_getPHPNumber' ), $code );
private function _getPHPNumber( $matches ) {
return ( $this->_getHtmlCode( $matches[0], PHP::$Colors['number'] ) );
}
private function _getHtmlCode( $text, $color ) {
$rows = array_filter( explode( "\n", $text ) );
$count = count( $rows );
$output = '';
for ( $i = 0; $i < $count; $i++ ) {
$n = ( $count > 1 ? ( $i != $count - 1 ? "\n" : "" ) : "" );
$output .= "<span style=\"color:{$color};\">{$rows[$i]}</span>{$n}";
}
return ( $output );
}
上面的输出如下所示:
<span style="color:#FF0000;">152</span>
它工作得很好,除非数字是 0,它会从输出中删除。导致问题的$rows = array_filter( explode( "\n", $text ) );
行在_getHtmlCode
. 确切的原因是array_filter
。如果我删除它,我的输出会完全损坏,但会出现 0。如果我离开它,0 将被删除。
有想法该怎么解决这个吗?