2

i have invoke from cli and php script like that:

# php test.php

the php script content:

<?php
echo chr(201);

that's the square corner on the top left for CLI

but it shows É character and expected: symbol
(sorry for the image I do not find this character for the post).

frame example bad output

I have try to use other method to get it working but nothing of it work:

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

The PHP file is UTF-8 without BOM.

terminal setting:

enter image description here

Update: Text Output:

<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

Update: ouput from drawbox function:

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

  • Why should it be? Did you save your file with the correct encoding? Does your terminal understand extended characters? Does it even use a font that supports them? etc. Also, showing "a random picture" doesn't help here, show your terminal that it can even show the character you're outputting, otherwise the problem is almost certainly your terminal settings, and probably not php. – Mike 'Pomax' Kamermans Aug 12 '20 at 22:29
  • @biesior i have upload picture becouse the reference in php manual have and image i cant copy a character from image: –  Aug 12 '20 at 23:55
  • @biesior https://www.php.net/manual/es/function.chr.php –  Aug 12 '20 at 23:55
  • @biesior http://www.asciitable.com/ can you see the code 201?? of ascci table i what it ouput but i get this stange character output https://i.stack.imgur.com/XbcoK.png –  Aug 12 '20 at 23:56
  • @biesior and i cant copy the strange character ... since you want me to edit my question and add an output that is not web, I have no way to get the result in text; everything is running from the CLI ??? –  Aug 12 '20 at 23:58
  • FYI: dedicated class is now available on GitHub for working with it: https://github.com/biesior/box-drawer – biesior Aug 15 '20 at 09:51

1 Answers1

1

In extended ASCII in PHP using cp1252 the chr(201) is É.

execute this code:

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

What you are actually looking for are Box Drawing characters which in Unicode has other values. You can copy abd paste it from the Wikipedia or just run this script to display all characters with numeric entities:

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

So I assume that you want to get something like this (run the snippet):

<!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>

I'm pretty sure that you will have long night to draw these boxes, I'd suggest some PP script for counting inner text's length LOL :D

Update

actually according to this old article drawings are even funny :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;
}

output

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

Set second param of drawBoxes() function $wrapInPre to true if you want to display this in HTML and false if it's for plain text.

biesior
  • 55,576
  • 10
  • 125
  • 182