highlight_string

highlight_string(string $string, bool $return = false): string|true

Built-in PHP syntax highlighting: highlight_string/highlight_file return <span>-colored HTML directly, no extension or third-party library needed — the only runtime-builtin highlighter among the ten languages.

C C23

C23
#include <stdio.h>
#include <string.h>

int main(void) {
    /* C has no built-in highlighter: wrap a known keyword by hand */
    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;
}
Output
int main(void) { <span class="kw">return</span> 42; }

C++ C++26

C++26
#include <iostream>
#include <regex>
#include <string>

int main() {
    // C++ has no built-in highlighter: std::regex substitution stands in
    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;
}
Output
<span class="kw">int</span> main() { <span class="kw">return</span> 42; }

C# v11.0.100

v11.0.100
// No built-in highlighter in the BCL: a regex replacement stands in
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);
Output
<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() {
	// No built-in highlighter: a hand-rolled keyword wrapper (external libraries are the norm)
	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>`))
}
Output
<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 has no built-in highlighter: a regex replacement stands in
        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);
    }
}
Output
<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
// No built-in highlighter: a hand-rolled keyword wrapper (highlight.js is the de-facto library)
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);
Output
<span class="kw">function</span> hello() { <span class="kw">return</span> 42; }

PHP v8.6.0

v8.6.0
// highlight_string returns HTML markup colored by the built-in highlighter
$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, "&lt;?php"), true), "\n";
Output
has span markup: true
has keyword echo: true
escapes open tag: true

Python v3.15.2

v3.15.2
# No built-in highlighter: a hand-rolled keyword wrapper (Pygments is the de-facto library)
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"))
Output
<span class="kw">def</span> hello(): <span class="kw">return</span> 42

Rust v1.99.1

v1.99.1
fn main() {
    // No built-in highlighter in std: a manual keyword replacement stands in
    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);
}
Output
<span class="kw">fn</span> hello() -> i32 { <span class="kw">return</span> 42; }

TypeScript v26.13.1

v26.13.1
// No built-in highlighter: a hand-rolled keyword wrapper (highlight.js is the de-facto library)
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);
Output
<span class="kw">function</span> hello() { <span class="kw">return</span> 42; }

Language differences

PHP: highlight_string/highlight_file are language builtins (the manual states no extension is required) returning <span style>-colored HTML; the manual also states the HTML structure may change between versions (overhauled in 8.3, return type narrowed to string|true in 8.4), so this entry only asserts three derived facts (contains <span> markup / contains the keyword / escapes the open tag) and never presets color values. Distinctive: among the ten languages only PHP builds syntax highlighting into the runtime.

Python: no builtin — Pygments is the de-facto third-party standard; the demo hand-wraps keywords.

Go: no builtin — external libraries are the norm; the demo wraps keywords with a regex.

Rust: no builtin in std — syntect is the ecosystem solution; the demo replaces manually.

Java: no built-in JDK highlighter — the demo uses regex replacement.

C#: no built-in BCL highlighter — the demo uses regex replacement.

C/C++: no builtin — production tools build on compiler front ends (libclang) or external libraries; the demo is a minimal hand-rolled replacement.

JavaScript/TypeScript: no builtin — highlight.js / Prism.js are the ecosystem standards; the demo hand-wraps keywords with a regex.