在一行内使用highlight_string函数

本文介绍了一个PHP类,该类能够对输入的代码进行格式化,并提供选项以实现代码高亮显示及显示行号的功能。通过使用highlight_string函数,可以有效支持多种编程语言的语法高亮。

This class show a code formated.
Allow options for to format.
Options: highlight code and to show line number

<?php

class
Code
{

function
printCode($code, $high_light = 0, $lines_number = 0
)
{
if (!
is_array($code)) $code = explode("/n", $code
);

$count_lines = count($code
);

foreach (
$code as $line => $code_line
) {

if (
$lines_number) $r1 = "<span class=/"lines_number/">".($line + 1)." </span>"
;

if (
$high_light
) {
if (
ereg("</?(php)?[^[:graph:]]", $code_line
)) {
$r2 = highlight_string($code_line, 1)."<br />"
;
} else {

$r2 = ereg_replace("(&lt;/?php&nbsp;)+", "", highlight_string("<?php ".$code_line, 1))."<br />"
;

}
} else {
$r2 = (!$line) ? "<pre>" : ""
;
$r2 .= htmlentities($code_line
);
$r2 .= ($line == ($count_lines - 1)) ? "<br /></pre>" : ""
;
}

$r .= $r1.$r2
;

}

echo
"<div class=/"code/">".$r."</div>"
;
}
}

?>

忘记你之前写的程序,基于我的如下程序做修改,使得整个关键词都能高亮,而不是只高亮首字母:" highlight color highlight BeginEndPair guibg=#00FF00 ctermbg=10 guifg=#000000 ctermfg=0 " Add Ctrl+F2 key mapping with debugging feedback nnoremap <silent> <C-F2> :call HighlightBeginEnd()<CR> " Toggle for debug messages (1 = on, 0 = off) let g:highlight_debug = 1 " Define highlight function with debugging function! HighlightBeginEnd() " Debug: Function started if g:highlight_debug | echom "HighlightBeginEnd() triggered" | endif " Get word under cursor let current_word = expand('<cword>') if g:highlight_debug | echom "Current word: '" . current_word . "'" | endif " Only process 'begin' keyword if current_word !=# 'begin' if g:highlight_debug | echom "Not 'begin' - aborting" | endif return endif if current_word !=# 'end' if g:highlight_debug | echom "Not 'end' - aborting" | endif return endif " Clear previous highlights call clearmatches() if g:highlight_debug | echom "Cleared existing highlights" | endif " Save original cursor position execute "normal! b" let save_pos = getcurpos() if g:highlight_debug | echom "Original position: line " . save_pos[1] . ", col " . save_pos[2] | endif if g:highlight_debug | echom "Original position: line " . save_pos[0] . ", col " . save_pos[0] | endif " Use matchit to find matching end try " Debug: Attempting match jump if g:highlight_debug | echom "Attempting jump to match..." | endif " Jump to matching position " silent! normal! % " echom "use matchit internal func to match" " call matchit#Match_wrapper('',0,0) " execute "normal! %" if g:highlight_debug | echom "call searchpairpos ..." | endif let match_pos = searchpairpos( '\<begin\>', '', '\<end\>', 'nW', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string\\|comment"' ) if g:highlight_debug | echom "call done ..." | endif if g:highlight_debug | echom "New1 position:line " . match_pos[0] . ", col " . match_pos[1] | endif " Check if jump was successful " execute "normal! b" " let match_pos = getcurpos() if match_pos[0] == save_pos[1] && match_pos[1] == save_pos[2] if g:highlight_debug | echom "No match found!" | endif return endif if g:highlight_debug echom "Match found at line " . match_pos[0] . ", col " . match_pos[1] endif " Highlight original begin let begin_hl = matchadd('BeginEndPair', '\%' . save_pos[1] . 'l\%' . save_pos[2] . 'c') if g:highlight_debug | echom "Added begin highlight" | endif " Highlight matching end let end_hl = matchadd('BeginEndPair', '\%' . match_pos[0] . 'l\%' . match_pos[1] . 'c') if g:highlight_debug | echom "Added end highlight" | endif finally " Restore original cursor position call setpos('.', save_pos) if g:highlight_debug | echom "Cursor restored" | endif endtry if g:highlight_debug | echom "Operation completed successfully" | endif endfunction
最新发布
07-26
实数格式识别 【问题描述】 合法的实数书写格式分一般格式和科学格式两种。分别描述如下: 一般格式为常见的书写格式,分为整数部分和小数部分两部分,中间分用小数点.分隔。整数部分最开始可能含有正号或负号,之后为不含前导零的数字串;小数部分是由0-9十种字符组成的任意长的字符串。当小数部分为0时,小数部分和小数点可以省略。 科学格式由系数部分和指数部分两部分组成,中间用英文字母E分隔。系数部分为实数书写的一般格式;指数部分为可带有正负号数字串。 例如,+2、-1.56为一般格式实数,而6.2E-2、-9E8为科学格式实数。 只有小数点而没有小数部分的书写格式为不合法,例如,23.,23.E16均为不合法的实数书写格式。 编程分析哪些数的书写是正确的,是用哪种方式书写的。 【入形式】 入文件为当前目录下的real.in。该文件包含一个字符串(长度不超过20个字符),以回车符结束,表示一个数据(无多余空格)。 【输出形式】 输出文件为当前目录下的real.out。该文件有一行。如果入数据的书写是非法的,输出Wrong;如果入数据是用一般格式书写的,输出“Format1”;如果该数据是用科学格式书写的,输出“Format2”。输出的末尾均要以一个回车符作为结束。 【入样例1】 +1.23 【输出样例1】 Format1 【入样例2】 -5.1.1 【输出样例2】 Wrong 【入样例3】 -5.1E-2 【输出样例3】 Format2 【时间限制】 1s 【空间限制】 65536KB 【上传文件】 上传c语言源程序,文件名为real.c。 -------------------------------------------------------------------------------- Upload Your source File(s) : Note :Your program can be written with the programing language(s) as below C(.c): your source filename is ''real.c''
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值