highlight_string
highlight_string(string $string, bool $return = false): string|true
PHP 内建语法高亮:highlight_string/highlight_file 直接返回 <span> 着色的 HTML,无需任何扩展或第三方库——十语言中唯一的运行时内建高亮。
C C23
C23
#include <stdio.h>
#include <string.h>
int main(void) {
/* C 无内建高亮:手动包裹已知关键字 */
const char *code = "int main(void) { return 42; }";
const char *kw = "return";
const char *p = strstr(code, kw);
printf("%.*s<span class=\"kw\">%s</span>%s\n", (int)(p - code), code, kw, p + strlen(kw));
return 0;
}
输出
int main(void) { <span class="kw">return</span> 42; }
C++ C++26
C++26
#include <iostream>
#include <regex>
#include <string>
int main() {
// C++ 无内建高亮:以 std::regex 替换代替
std::string code = "int main() { return 42; }";
std::regex kw(R"(\b(int|return|if|else|for)\b)");
std::cout << std::regex_replace(code, kw, R"(<span class="kw">$1</span>)") << "\n";
return 0;
}
输出
<span class="kw">int</span> main() { <span class="kw">return</span> 42; }
C# v11.0.100
v11.0.100
// BCL 无内建高亮:以正则替换代替
string code = "class Hello { int Run() { return 42; } }";
string highlighted = System.Text.RegularExpressions.Regex.Replace(
code, @"\b(class|int|return)\b", "<span class=\"kw\">$1</span>");
Console.WriteLine(highlighted);
输出
<span class="kw">class</span> Hello { <span class="kw">int</span> Run() { <span class="kw">return</span> 42; } }
Go v1.27.4
v1.27.4
package main
import (
"fmt"
"regexp"
)
func main() {
// 无内建高亮:手写关键字包裹(外部库才是常态)
re := regexp.MustCompile(`\b(func|return|package|import|if|else)\b`)
fmt.Println(re.ReplaceAllString("func hello() int { return 42 }", `<span class="kw">$1</span>`))
}
输出
<span class="kw">func</span> hello() int { <span class="kw">return</span> 42 }
Java v27.0.1
v27.0.1
public class Main {
public static void main(String[] args) {
// Java 无内建高亮:以正则替换代替
String code = "class Hello { int run() { return 42; } }";
String out = code.replaceAll("\\b(class|int|return)\\b", "<span class=\"kw\">$1</span>");
System.out.println(out);
}
}
输出
<span class="kw">class</span> Hello { <span class="kw">int</span> run() { <span class="kw">return</span> 42; } }
JavaScript v26.13.1
v26.13.1
// 无内建高亮:手写关键字包裹(highlight.js 是事实标准库)
const code = "function hello() { return 42; }";
const out = code.replace(/\b(function|return|const|let|var|if|else)\b/g, '<span class="kw">$1</span>');
console.log(out);
输出
<span class="kw">function</span> hello() { <span class="kw">return</span> 42; }
PHP v8.6.0
v8.6.0
// highlight_string 返回内建高亮着色的 HTML 标记
$html = highlight_string('<?php echo "Hello";', true);
echo "has span markup: ", var_export(str_contains($html, "<span"), true), "\n";
echo "has keyword echo: ", var_export(str_contains($html, "echo"), true), "\n";
echo "escapes open tag: ", var_export(str_contains($html, "<?php"), true), "\n";
输出
has span markup: true has keyword echo: true escapes open tag: true
Python v3.15.2
v3.15.2
# 无内建高亮:手写关键字包裹(Pygments 是事实标准库)
import re
def highlight(code):
return re.sub(r"\b(def|return|for|in|if|else|print)\b", r'<span class="kw">\1</span>', code)
print(highlight("def hello(): return 42"))
输出
<span class="kw">def</span> hello(): <span class="kw">return</span> 42
Rust v1.99.1
v1.99.1
fn main() {
// 标准库无内建高亮:以手动关键字替换代替
let code = "fn hello() -> i32 { return 42; }";
let highlighted = code.replace("fn ", "<span class=\"kw\">fn</span> ")
.replace("return ", "<span class=\"kw\">return</span> ");
println!("{}", highlighted);
}
输出
<span class="kw">fn</span> hello() -> i32 { <span class="kw">return</span> 42; }
TypeScript v26.13.1
v26.13.1
// 无内建高亮:手写关键字包裹(highlight.js 是事实标准库)
const code: string = "function hello() { return 42; }";
const out: string = code.replace(/\b(function|return|const|let|var|if|else)\b/g, '<span class="kw">$1</span>');
console.log(out);
输出
<span class="kw">function</span> hello() { <span class="kw">return</span> 42; }
语言差异说明
PHP:highlight_string/highlight_file 是语言内建高亮(手册明示不需要任何扩展),返回 <span style> 着色的 HTML;手册同时明示 HTML 结构随版本可变(8.3 大改、8.4 返回类型收窄为 string|true),故本条目只断言「含 <span> 结构 / 含关键字 / 转义开标签」三个派生事实,绝不预置颜色值。特色:十语言中只有 PHP 把语法高亮做进了运行时。
Python:无内建——事实标准是第三方 Pygments;示例为手写关键字包裹。
Go:无内建——外部库为常态;示例手写正则包裹。
Rust:std 无内建——生态方案是 syntect;示例手动替换。
Java:JDK 无内建高亮——示例正则替换。
C#:BCL 无内建高亮——示例正则替换。
C/C++:无内建——生产环境基于编译器前端(libclang)或外部库;示例最小手写替换。
JavaScript/TypeScript:无内建——highlight.js / Prism.js 是生态事实标准;示例手写正则包裹。