Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9190413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:27:19+00:00 2026-06-17T20:27:19+00:00

I decided to use Pygments for a website I’m working on, but my lack

  • 0

I decided to use Pygments for a website I’m working on, but my lack of terminal knowledge is amazing.

I want to use pygmentize to highlight syntax in blog posts, but as they are stored in database I can’t just pass filename to it. Is there any way I can pass string into it?

If not, I will have to save post contents in a temp file, pygmentize it and load into database but this adds overhead that I would really like to avoid if at all possible.

I don’t see CLI documentation saying anything about it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:27:20+00:00Added an answer on June 17, 2026 at 8:27 pm

    The man page says it reads from stdin if infile is omitted and it writes to stdout if outfile is omitted.

    So on the cmdline you would type:

    $ pymentize -l php -f html
    <?php
    
    echo 'hello world!';
    ^D // type: Control+D
    

    pymentize would output:

    <div class="highlight"><pre><span class="cp">&lt;?php</span>
    
    <span class="k">echo</span> <span class="s1">&#39;hello world!&#39;</span><span class="p">; </span>
    </pre></div>
    

    If you’ll run this with from PHP you’ll have to start pygmentize using proc_open() as you’ll have to write to stdin of it. Here comes a short example how to do it:

    echo pygmentize('<?php echo "hello world!\n"; ?>');
    
    /**
     * Highlights a source code string using pygmentize
     */
    function pygmentize($string, $lexer = 'php', $format = 'html') {
        // use proc open to start pygmentize
        $descriptorspec = array (
            array("pipe", "r"), // stdin
            array("pipe", "w"), // stdout
            array("pipe", "w"), // stderr
        );  
    
        $cwd = dirname(__FILE__);
        $env = array();
    
        $proc = proc_open('/usr/bin/pygmentize -l ' . $lexer . ' -f ' . $format,
            $descriptorspec, $pipes, $cwd, $env);
    
        if(!is_resource($proc)) {
            return false;
        }   
    
        // now write $string to pygmentize's input
        fwrite($pipes[0], $string);
        fclose($pipes[0]);
    
        // the result should be available on stdout
        $result = stream_get_contents($pipes[1]);
        fclose($pipes[1]);
    
        // we don't care about stderr in this example
    
        // just checking the return val of the cmd
        $return_val = proc_close($proc);
        if($return_val !== 0) {
            return false;
        }   
    
        return $result;
    }
    

    Btw, pygmentize is pretty cool stuff! I’m using it too 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We decided to use mongodb for some web application (instead of mysql) but want
I have decided to use ASP.NET MVC for a website project and want to
I decided to use time instead of string date in MySQL, but I want
I am working on a database access layer project and decided to use Linq
I decided to use Eigen library in my project. But it is not clear
I decided to use jquery-ui Autocomplete Combobox, but until now I didn't noticed that
If I decided to use some javascipt in my website like $('#body').load(URL); or $.get(URL,
We use SVN at work, but for my personal projects I decided to use
I just started working with Drupal for a while and decided to use this
We're working on quite big project and decided use GIT for version control. I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.