PHP: Regex preg_replace_callback to match all numbers in PHP -
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 = ''; ( $i = 0; $i < $count; $i++ ) { $n = ( $count > 1 ? ( $i != $count - 1 ? "\n" : "" ) : "" ); $output .= "<span style=\"color:{$color};\">{$rows[$i]}</span>{$n}"; } return ( $output ); } the output of above this:
<span style="color:#ff0000;">152</span> it works great, except if number 0, gets removed output. line causing issue $rows = array_filter( explode( "\n", $text ) ); in _gethtmlcode. exact cause array_filter. if remove it, output gets broken, 0's appear. if leave it, 0's removed.
any ideas on how fix this?
the man page array_filter has answer
if no callback supplied, all *entries of input equal false (see* converting boolean) be *removed.*
and believe 0 evaluates false.
i can't test @ moment believe work
$rows = array_filter( explode( "\n", $text ), 'is_string' );
Comments
Post a Comment