php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80793 highlight_string
Submitted: 2021-02-24 03:57 UTC Modified: 2021-02-26 16:47 UTC
From: icarosnet at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: Irrelevant OS: all
Private report: No CVE-ID: None
 [2021-02-24 03:57 UTC] icarosnet at gmail dot com
Description:
------------
hello any way to get highlight_string working in CLI?

Test script:
---------------
highlight_cli 

Expected result:
----------------
highlight

Actual result:
--------------
not exist

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-02-24 03:59 UTC] requinix@php.net
-Status: Open +Status: Not a bug -Type: Feature/Change Request +Type: Bug
 [2021-02-24 03:59 UTC] requinix@php.net
Check your highlight.* php.ini settings.
 [2021-02-24 04:13 UTC] icarosnet at gmail dot com
i have check the php.init setting and only have this commented:

; Colors for Syntax Highlighting mode.  Anything that's acceptable in
; <span style="color: ???????"> would work.
; http://php.net/syntax-highlighting
;highlight.string  = #DD0000
;highlight.comment = #FF9900
;highlight.keyword = #007700
;highlight.default = #0000BB
;highlight.html    = #000000

the problem, not the limitation that php has in CLI is that "highlight_string" returns html code even when run from a terminal. is there a way to do it or recreate it for CLI?
 [2021-02-24 04:19 UTC] icarosnet at gmail dot com
I am interested in this feature to run in cli, because i want implement in other of library: https://github.com/arcanisgk/BOH-Basic-Output-Handler

currently only works on web, but if "highlight_string" works correctly in CLI I can extend the support.
 [2021-02-24 04:36 UTC] 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.
 [2021-02-24 04:44 UTC] 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 05:08 UTC] 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 05:19 UTC] 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:23 UTC] 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 06:35 UTC] icarosnet at gmail dot com
Thanks a lot; I have made a small adaptation to my library based on what you have shared with me and I see that it works partially, I will do my part and verify what is not working correctly. I leave you the image of the output by cli:

https://i.imgur.com/vjBjuVQ.png
 [2021-02-26 16:47 UTC] icarosnet at gmail dot com
the library is now ready:
https://github.com/arcanisgk/BOH-Basic-Output-Handler
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon May 23 07:03:48 2022 UTC