Bug #80793 [Nab]: highlight_string

From: Date: Wed, 24 Feb 2021 05:54:18 +0000
Subject: Bug #80793 [Nab]: highlight_string
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-232378@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80793&edit=1

 ID:                 80793
 Updated by:         requinix@php.net
 Reported by:        icarosnet at gmail dot com
 Summary:            highlight_string
 Status:             Not a bug
 Type:               Bug
 Package:            Unknown/Other Function
 Operating System:   all
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

https://gist.github.com/requinix/6895f9abeb90022f729a8b58a160be60


Previous Comments:
------------------------------------------------------------------------
[2021-02-24 05:23:58] requinix@php.net

Please re-read the documentation for token_get_all.
https://www.php.net/token-get-all

If you have any more questions about your code, see
https://www.php.net/support.php

------------------------------------------------------------------------
[2021-02-24 05:19:18] icarosnet at gmail dot com

I have verified both the Zend engine topic and the get all token function; but when trying to
implement it I get an error:

Warning: token_name() expects parameter 1 to be int, string given in
C:\xampp\htdocs\dev\t5\BOH-Basic-Output-Handler\src\OutputHandler.php on line 320
NULL
string(1) "="

Warning: token_name() expects parameter 1 to be int, string given in
C:\xampp\htdocs\dev\t5\BOH-Basic-Output-Handler\src\OutputHandler.php on line 320
NULL
string(1) "["

Do you have any idea how to solve the error of these signs?

The code you run to test is as follows:

        $test = '
        <?php ' . PHP_EOL . '
        $array=["data1","data2"];
        ' . PHP_EOL . ' ?>
        ';
        
        $tokenstring = token_get_all($test);
        foreach ($tokenstring as $key => $arraytoken) {
            $tokenname = token_name($arraytoken[0]);
            echo '<pre>';
            echo var_dump($tokenname, $arraytoken);
            echo '</pre>';
        }

------------------------------------------------------------------------
[2021-02-24 05:08:25] requinix@php.net

> you simply have to change the span tag for the php
Actually no, it is not that simple.

But you're missing the point. This isn't a matter of whether it *can* be done. This is a
matter of whether it *should* be done. Syntax highlighting is complicated and PHP's built-in
functionality serves the purpose as quickly as it can to some minimumm extent. Good highlighting
should allow for more customization than just the four categories PHP has (string, comment, keyword,
html), possibly even doing a certain amount of lexing beyond the initial tokenization, and
that's a lot of work to ask from the core developers for a feature that, honestly, doesn't
get used much.

This is best solved by a third-party library.

If you still feel strongly that this functionality needs to be added/improved within PHP itself,
check out the RFC process.
https://wiki.php.net/rfc/howto

------------------------------------------------------------------------
[2021-02-24 04:44:35] icarosnet at gmail dot com

well, I don't know if you can do it, to include this request in a vote, to see if they take it
out within PHP. It seems to me that it would be the same as with html, you simply have to change the
span tag for the php escapes in opening and closing, I would do it but I don't have access ...

------------------------------------------------------------------------
[2021-02-24 04:36:11] requinix@php.net

Terminal coloring varies by terminal and operating system, though cmd in Windows 10 now seems to
support the common ANSI \e[ sequences so that helps.

With a small amount of creativity, you can alter the string returned by highlight_string() to use
the proper escape sequences.

Otherwise, if there isn't already a library out there to do this for you (and I'm 95% sure
there is), the internals behind highlight_string() are not complicated so you could do it yourself
using token_get_all().
https://github.com/php/php-src/blob/PHP-8.0.3/Zend/zend_highlight.c#L84

Because honestly, this sort of thing really is best handled by a library rather than bundled into
PHP.

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=80793


--
Edit this bug report at https://bugs.php.net/bug.php?id=80793&edit=1


Thread (12 messages)

« previous php.bugs (#232378) next »