定义 PHP 列表

定义 PHP 列表

问题:

1) 收到以下错误消息:(a) 软件包列表错误:无法加载请求的语言 (b) 软件包列表错误:语言 phpalsodigit 未定义。

2)无法弄清楚如何为函数/类/方法(PDO、sprintf、setAttribute、getMessage)和变量(以 $ 开头的所有内容)着色。

最小工作示例(MWE):

\documentclass{scrreprt}
\usepackage{color}
\definecolor{editorGray}{cmyk}{0.6, 0.55, 0.55, 0.2}
\definecolor{editorPurple}{cmyk}{0.4, 0.9, 0, 0}
\definecolor{editorWhite}{cmyk}{0, 0, 0, 0}
\definecolor{editorOrange}{cmyk}{0, 0.8, 1, 0}
\definecolor{editorBlue}{cmyk}{0.8, 0.7, 0, 0}
\definecolor{editorPink}{cmyk}{0.01, 0.81, 0.05, 0}
\usepackage{upquote}
\usepackage{listings}

\lstdefinelanguage{PHP}{
        language=php,
        sensitive=true, 
        alsoletter={<>=-},
        otherkeywords={require_once, try, new, catch, die},  
        ndkeywords={setAttribute, getMessage},  
        morecomment=[s]{<!--}{-->},
        tag=[s]
}

\lstset{%
    % Basic design
    backgroundcolor=\color{editorWhite},
    basicstyle={\small\ttfamily},   
    frame=l,
    % Line numbers
    xleftmargin={0cm},
    numbers=left,
    stepnumber=1,
    firstnumber=1,
    numberfirstline=true,
    % Code design   
    keywordstyle=\color{editorPink}\bfseries,
    commentstyle=\color{editorGray}\ttfamily,
    ndkeywordstyle=\color{editorBlue}\bfseries,
    stringstyle=\color{editorPurple},
    % Code
    language=PHP
    alsodigit={.:;},
    tabsize=2,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    extendedchars=true,
    breaklines=false,        
    % Support for German umlauts
    literate=%
    {Ö}{{\"O}}1
    {Ä}{{\"A}}1
    {Ü}{{\"U}}1
    {ß}{{\ss}}1
    {ü}{{\"u}}1
    {ä}{{\"a}}1
    {ö}{{\"o}}1
}
\begin{document}
    \begin{lstlisting}
<?php
    // Includes database settings
    require_once('../db.php');

    // Connects to database
    try {
        $dbh = new PDO(
                sprintf(
                    'mysql:host=%s;dbname=%s;port=%s;charset=%s',
                    $settings['host'],
                    $settings['name'],
                    $settings['port'],
                    $settings['charset']
                ),
                $settings['username'],
                $settings['password']
        );
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    // Prints raised errors
    catch (PDOException $e) {
        die('Error: ' . $e->getMessage());
    }
?>
    \end{lstlisting}
\end{document}

期望的结果:

  1. 规避定义PHP语言的错误。
  2. 函数/类/方法应该是蓝色,变量应该是橙色。

相关内容