【问题标题】:chr not show ascii extended symbol from CLI requestchr 不显示来自 CLI 请求的 ascii 扩展符号
【发布时间】:2020-08-12 22:23:59
【问题描述】:

我已经像这样从 cli 和 php 脚本调用:

# php test.php

php脚本内容:

<?php
echo chr(201);

这是 CLI 左上角的方角

但它显示É 字符和预期:
对不起,我没有为帖子找到这个角色)。

我已经尝试使用其他方法让它工作,但没有任何工作:

<?php
echo mb_convert_encoding(chr(201), 'UTF-8', 'ISO-8859-1');
<?php
echo utf8_encode(chr(201));

PHP 文件是 UTF-8,没有 BOM。

终端设置:

更新:文本输出:

<0x1b[1;0m
array [
    'idprocess' => string(11): 'c-cron-0001',
    'idform' => string(11): 'c-cron-0001',
]
<0x1b>[0m
<0x1b>[1;0m
array [
    'idprocess' => string(11): 'c-cron-0004',
    'idform' => string(11): 'c-cron-0004',
]
<0x1b>[0m
<0x1b>[1;0m
array [
    'idprocess' => string(11): 'c-cron-0005',
    'idform' => string(11): 'c-cron-0005',
]
<0x1b>[0m
<0x1b>[1;0m
array [
    'idprocess' => string(11): 'c-cron-0006',
    'idform' => string(11): 'c-cron-0006',
]
<0x1b>[0m
<0x1b>[1;0m
array [
    'idprocess' => string(11): 'c-cron-0007',
    'idform' => string(11): 'c-cron-0007',
]
<0x1b>[0m

更新:drawbox 函数的输出:

╔══════════════════════════════════════════════════════════════════════════════╗
║ <0x1b>[1;0m                                                                       ║
║ array [                                                                      ║
║     'idprocess' => string(11): 'c-cron-0001',                                ║
║     'idform' => string(11): 'c-cron-0001',                                   ║
║ ]                                                                            ║
║ <0x1b>[0m                                                                         ║
║ <0x1b>[1;0m                                                                       ║
║ array [                                                                      ║
║     'idprocess' => string(11): 'c-cron-0004',                                ║
║     'idform' => string(11): 'c-cron-0004',                                   ║
║ ]                                                                            ║
║ [0m                                                                         ║
║ [1;0m                                                                       ║
║ array [                                                                      ║
║     'idprocess' => string(11): 'c-cron-0005',                                ║
║     'idform' => string(11): 'c-cron-0005',                                   ║
║ ]                                                                            ║
║ <0x1b>[0m                                                                         ║
║ <0x1b>[1;0m                                                                       ║
║ array [                                                                      ║
║     'idprocess' => string(11): 'c-cron-0006',                                ║
║     'idform' => string(11): 'c-cron-0006',                                   ║
║ ]                                                                            ║
║ <0x1b>[0m                                                                         ║
║ <0x1b>[1;0m                                                                       ║
║ array [                                                                      ║
║     'idprocess' => string(11): 'c-cron-0007',                                ║
║     'idform' => string(11): 'c-cron-0007',                                   ║
║ ]                                                                            ║
║ <0x1b>[0m                                                                         ║
║                                                                              ║
╚══════════════════════════════════════════════════════════════════════════════╝

【问题讨论】:

  • 为什么会这样?您是否使用正确的编码保存文件?您的终端是否理解扩展字符?它甚至使用支持它们的字体吗?另外,在这里显示“随机图片”也无济于事,请向您的终端显示它甚至可以显示您正在输出的字符,否则问题几乎可以肯定是您的终端设置,可能不是 php。
  • @biesior 我上传了图片,因为 php 手册中有参考资料,而且图片我无法从图片中复制字符:
  • @biesior asciitable.com 你能看到代码 201 吗? ascci 表的我输出了什么,但我得到了这个 stange 字符输出i.stack.imgur.com/XbcoK.png
  • @biesior,我无法复制奇怪的字符......因为你要我编辑我的问题并添加一个不是网络的输出,所以我无法以文本形式获得结果;一切都从 CLI 运行???

标签: php ascii chr


【解决方案1】:

在 PHP 中使用 cp1252 的扩展 ASCII 中,chr(201)É

执行此代码:

<?php
for ($i = 0; $i <= 255; $i++) {
    echo "$i: " . htmlentities(chr($i), ENT_QUOTES, 'cp1252') . "<br />";
}

您真正要查找的是 Box Drawing characters,它在 Unicode 中具有其他值。您可以从 Wikipedia 复制 abd 并粘贴它,或者只运行此脚本以显示所有带有数字实体的字符:

<?php
$mains = [250, 251, 252, 253, 254, 255, 256, 257];
$subs = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F'];
echo '<table>';
foreach ($mains as $main){
    foreach ($subs as $sub){
        $code = $main.$sub;
        echo "
            <tr>
                <td><pre>&amp;#x{$code};</pre> </td>
                <td>&#x{$code};</td>
            </tr>" ;
    }
}
echo '</table>';
HTML        char
-----------------
...
&#x2552;    ╒
&#x2553;    ╓
&#x2554;    ╔
&#x2555;    ╕
&#x2556;    ╖
&#x2557;    ╗
...

所以我假设你想得到这样的东西(运行 sn-p):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <style>
        .monospace {
            font-family: monospace;
        }
    </style>
</head>
<body>
<div class="monospace">
    &#x2554;&#x2550;&#x2550;&#x2550;&#x2550;&#x2550;&#x2550;&#x2550;&#x2550;&#x2550;&#x2550;&#x2550;&#x2557;<br>
    &#x2551; Some text &#x2551;<br>
    &#x255A;&#x2550;&#x2550;&#x2550;&#x2550;&#x2550;&#x2550;&#x2550;&#x2550;&#x2550;&#x2550;&#x2550;&#x255D;
</div>

</body>
</html>

我很确定你会花很长时间来绘制这些框,我建议一些 PP 脚本来计算内部文本的长度 LOL:D

更新

其实照this old article画的还蛮搞笑的:D

<?php

drawBoxes(
    [
        'Header',
        'Hello, World!',
        'Box drawing is alive after all these years!!!! :D',
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum arcu.'
    ],
    true,
    true
);

function drawBoxes(array $lines, $isFirstLineHeader = false, $wrapInPre = false)
{
    $tl = html_entity_decode('╔', ENT_NOQUOTES, 'UTF-8'); // top left corner
    $tr = html_entity_decode('╗', ENT_NOQUOTES, 'UTF-8'); // top right corner
    $bl = html_entity_decode('╚', ENT_NOQUOTES, 'UTF-8'); // bottom left corner
    $br = html_entity_decode('╝', ENT_NOQUOTES, 'UTF-8'); // bottom right corner
    $v = html_entity_decode('║', ENT_NOQUOTES, 'UTF-8');  // vertical wall
    $h = html_entity_decode('═', ENT_NOQUOTES, 'UTF-8');  // horizontal wall

    $hs = html_entity_decode('─', ENT_NOQUOTES, 'UTF-8');  // horizontal wall single
    $ls = html_entity_decode('╟', ENT_NOQUOTES, 'UTF-8');  // right separator
    $rs = html_entity_decode('╢', ENT_NOQUOTES, 'UTF-8');  // right separator

    $longest = 0;
    foreach ($lines as $line) {
        $len = strlen($line);
        if ($len > $longest) {
            $longest = $len;
        }
    }
    $preStart = $wrapInPre ? "<pre style='font-family: monospace'>" . PHP_EOL : '';
    $preEnd = $wrapInPre ? PHP_EOL . "</pre>" : '';
    echo $preStart . $tl . str_repeat($h, $longest + 2) . $tr . PHP_EOL;
    $i = 0;
    foreach ($lines as $line) {
        $addEmpty = '';
        $len = strlen($line);
        if ($len < $longest) {
            $addEmpty = str_repeat(' ', $longest - $len);
        }
        echo $v . ' ' . $line . $addEmpty . ' ' . $v;
        if ($isFirstLineHeader && $i == 0) {
            echo PHP_EOL . $ls . str_repeat($hs, $longest + 2) . $rs . PHP_EOL;
        } else {
            echo PHP_EOL;
        }
        $i++;
    }

    echo $bl . str_repeat($h, $longest + 2) . $br . $preEnd;
}

输出

╔═══════════════════════════════════════════════════════════════════════════╗
║ Header                                                                    ║
╟───────────────────────────────────────────────────────────────────────────╢
║ Hello, World!                                                             ║
║ Box drawing is alive after all these years!!!! :D                         ║
║ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum arcu. ║
╚═══════════════════════════════════════════════════════════════════════════╝

如果您想以 HTML 格式显示,则将 drawBoxes() 函数 $wrapInPre 的第二个参数设置为 true,如果是纯文本,则将其设置为 false

【讨论】:

  • 只是最后的想法。用CSS不是更容易吗? :D
  • PHP-CLI 不支持 CSS...XD
  • 由于某种原因,它看起来像这样:imgur.com/r1LveBH 空行或连续换行问题...
  • 粘贴您传递给函数的示例代码(作为代码,而不是图像!)
猜你喜欢
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
  • 2011-04-26
  • 1970-01-01
  • 2015-04-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多