蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

在 PHP 8.4 CLI 验证多字节长度截取、检索替换、格式化与转义散列

PHPWindows x64、PHP 8.4 本地 CLI 环境。explore-ref-string.php
explore-ref-string.php111 行
<?php
declare(strict_types=1);

$cases = [];

// Case 1: strlen vs mb_strlen, substr vs mb_substr, strrev
$zh = '蓝色天空网';
$lenActual = [
    'strlen_utf8_bytes' => strlen($zh),
    'mb_strlen_chars' => mb_strlen($zh, 'UTF-8'),
    'substr_ascii' => substr('PHP8.4-LTS', 0, 6),
    'mb_substr_zh' => mb_substr($zh, 0, 2, 'UTF-8'),
    'strrev_ascii' => strrev('PHP84'),
];
$lenExpected = [
    'strlen_utf8_bytes' => 15,
    'mb_strlen_chars' => 5,
    'substr_ascii' => 'PHP8.4',
    'mb_substr_zh' => '蓝色',
    'strrev_ascii' => '48PHP',
];
$cases[] = [
    'name' => 'length_slice_and_multibyte',
    'passed' => $lenActual === $lenExpected,
    'expected' => $lenExpected,
    'actual' => $lenActual,
];

// Case 2: strpos / stripos / str_replace / str_ireplace / PHP 8 str_* helpers
$text = 'Hello PHP 8.4 World';
$searchActual = [
    'strpos' => strpos($text, 'PHP'),
    'stripos' => stripos($text, 'world'),
    'str_replace' => str_replace('World', 'CLI', $text),
    'str_ireplace' => str_ireplace('hello', 'Hi', $text),
    'substr_count' => substr_count('ababa', 'ab'),
    'str_contains' => str_contains($text, '8.4'),
    'str_starts_with' => str_starts_with($text, 'Hello'),
    'str_ends_with' => str_ends_with($text, 'World'),
];
$searchExpected = [
    'strpos' => 6,
    'stripos' => 14,
    'str_replace' => 'Hello PHP 8.4 CLI',
    'str_ireplace' => 'Hi PHP 8.4 World',
    'substr_count' => 2,
    'str_contains' => true,
    'str_starts_with' => true,
    'str_ends_with' => true,
];
$cases[] = [
    'name' => 'search_replace_and_php8_helpers',
    'passed' => $searchActual === $searchExpected,
    'expected' => $searchExpected,
    'actual' => $searchActual,
];

// Case 3: trim / explode / implode / sprintf / number_format / str_getcsv
$parts = explode(',', 'php,string,utf8');
$fmtActual = [
    'trim' => trim("  PHP 8.4\t\n"),
    'explode' => $parts,
    'implode' => implode('|', $parts),
    'sprintf' => sprintf('%s %.1f (%04d)', 'PHP', 8.4, 27),
    'number_format' => number_format(12345.678, 2, '.', ','),
    'str_getcsv' => str_getcsv('php,8.4,"web,cli"', ',', '"', '\\'),
];
$fmtExpected = [
    'trim' => 'PHP 8.4',
    'explode' => ['php', 'string', 'utf8'],
    'implode' => 'php|string|utf8',
    'sprintf' => 'PHP 8.4 (0027)',
    'number_format' => '12,345.68',
    'str_getcsv' => ['php', '8.4', 'web,cli'],
];
$cases[] = [
    'name' => 'split_join_trim_and_format',
    'passed' => $fmtActual === $fmtExpected,
    'expected' => $fmtExpected,
    'actual' => $fmtActual,
];

// Case 4: htmlspecialchars / strip_tags / bin2hex / hex2bin / md5 / sha1
$escaped = htmlspecialchars('<a href="x">&</a>', ENT_QUOTES, 'UTF-8');
$secActual = [
    'escaped' => $escaped,
    'decoded' => htmlspecialchars_decode($escaped, ENT_QUOTES),
    'strip_tags' => strip_tags('<p>Hello <b>PHP</b></p>'),
    'bin2hex' => bin2hex('AB'),
    'hex2bin' => hex2bin('4142'),
    'md5' => md5('PHP'),
    'sha1' => sha1('PHP'),
];
$secExpected = [
    'escaped' => '&lt;a href=&quot;x&quot;&gt;&amp;&lt;/a&gt;',
    'decoded' => '<a href="x">&</a>',
    'strip_tags' => 'Hello PHP',
    'bin2hex' => '4142',
    'hex2bin' => 'AB',
    'md5' => '2fec392304a5c23ac138da22847f9b7c',
    'sha1' => '7daf403c7589f4927632ed3b6af762a992f09b78',
];
$cases[] = [
    'name' => 'escape_encode_and_hash',
    'passed' => $secActual === $secExpected,
    'expected' => $secExpected,
    'actual' => $secActual,
];

echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
本例需在 Windows x64、PHP 8.4 本地 CLI 环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、PHP 8.4 本地 CLI 环境。

入口文件:projects/php-ref-string-lab/explore-ref-string.php

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-ref-string.php,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:PHP

课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip

下载课程关联工程包:PHP

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1300.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习