php highlight_string,PHP: highlight_string - Manual

I've been working on a good replacement for the highlight_string() function; and here is what I've come up with so far:

function get_sourcecode_string($str, $return = false, $counting = true, $first_line_num = '1', $font_color = '#666'){

$str = highlight_string($str, TRUE);

$replace = array(

' '

'color="' => 'style="color: ',

'

' => '',

'' => '',

'' => '',

'' =>

''

);

foreach ($replace as $html => $xhtml){

$str = str_replace($html, $xhtml, $str);

}

// delete the first and the corresponding

$str = substr($str, 30, -9);

$arr_html      = explode('
', $str);

$total_lines   = count($arr_html);

$out           = '';

$line_counter  = 0;

$last_line_num = $first_line_num + $total_lines;

foreach ($arr_html as $line){

$line = str_replace(chr(13), '', $line);

$current_line = $first_line_num + $line_counter;

if ($counting){

$out .= ''

. str_repeat(' ', strlen($last_line_num) - strlen($current_line))

. $current_line

. ': ';

}

$out .= $line

. '
'."\n";

$line_counter++;

}

$out = ''."\n".$out.'."\n"';

if ($return){return $out;}

else {echo $out;}

}

?>

This function outputs valid XHTML 1.1 code by replacing font tags with span tags. You can also specify whether you want it to return or echo, output a line-count, the color of the line-count, and the starting line-count number.

Usage:

// $str = string with php

// $return = true (return) / false (echo)

//    default of false

// $counting = true (count) / false (don't count)

//    default of true

// $start = starting count number

//    default of '1'

// $color = count color with preceding #

//    defalut of '#666'

get_sourcecode_string($str, $return,   $counting, $start, $color);

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值