chr不显示来自CLI请求的ascii扩展符号

问题描述

我从cli和PHP脚本中调用的方式如下:

# PHP test.PHP

PHP脚本内容

<?PHP
echo chr(201);

这是CLI左上角的方形角

但它显示É字符并应为:

symbol


很抱歉,我在帖子中找不到该字符。)

frame example bad output

我尝试使用其他方法使其正常运行,但没有任何效果

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

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

终端设置:

enter image description here

更新:文本输出

<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

更新:从绘图盒功能输出

╔══════════════════════════════════════════════════════════════════════════════╗
║ <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                                                                         ║
║                                                                              ║
╚══════════════════════════════════════════════════════════════════════════════╝

enter image description here

解决方法

在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;    ╗
...

因此,我假设您想获得类似的内容(运行代码段):

<!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('╗','UTF-8'); // top right corner
    $bl = html_entity_decode('╚','UTF-8'); // bottom left corner
    $br = html_entity_decode('╝','UTF-8'); // bottom right corner
    $v = html_entity_decode('║','UTF-8');  // vertical wall
    $h = html_entity_decode('═','UTF-8');  // horizontal wall

    $hs = html_entity_decode('─','UTF-8');  // horizontal wall single
    $ls = html_entity_decode('╟','UTF-8');  // right separator
    $rs = html_entity_decode('╢','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设置为第二个参数。