Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

Problems with PHP tags (and code) in listings with HTML

General Tech Bugs & Fixes
Max. 2000 characters
Replies

usr_profile.png
Alice Davidson

User

( 6 months ago )

 

I want to highlight PHP and HTML in the same code. In my attempts I experience three challenges:

  1. Seems like listings treats <?php and ?> as one large HTML tag. How to tell listings that the <in <?php is not a HTML tag?
  2. Variable values (here 23 and 42) should have a different colour... Because of what I mentioned in (1) it seems like all code between <?php and ?> is treated as HTML.
  3. Variable names... They should have their own colour. I have fixed it with emph, but there must be a better way?

All help appreciated!

Best Regards Lars

\documentclass{article}
\usepackage{color}
\usepackage{listings, textcomp}

\definecolor{codecolor}{cmyk}{0.75,0.4,0,0}
\definecolor{codebg}{cmyk}{0.02,0.02,0.02,0.02}
\definecolor{codenumber}{cmyk}{0.2,0.1,0.1,0.2}
\definecolor{codecomment}{cmyk}{0.6,0,1,0}
\definecolor{codestring}{cmyk}{0,1,1,0}

\lstdefinelanguage{myhtml} {
  language = HTML,
  morecomment = [s]{<!--}{-->},
  morecomment = [l]{-->},
  otherkeywords = {=, href, target, alt, controls, action, method, src, width, height, type, class}
}

\lstdefinestyle{customphp}{
  captionpos = b,
  breaklines = true,
  breakatwhitespace = true;
  xleftmargin = \parindent,
  language = PHP,
  alsolanguage = myhtml,
  upquote = true,
  showstringspaces = false,
  numbers = left,
  basicstyle = \footnotesize\ttfamily\color{black},
  commentstyle = \color{codecomment},
  keywordstyle = \color{codecolor},
  numberstyle = \tiny\color{codenumber},
  stringstyle = \color{codestring}, 
  backgroundcolor = \color{codebg},
  tagstyle = \color{codecolor},
  emph = {$number},
  emphstyle = {\color{green}}
}

\begin{document}

\begin{lstlisting}[style=customphp]
<!doctype html>
<html>
<head>
    <title> My first PHP document </title>
</head>
<body>

<h1> Welcome to my first PHP document! </h1>

<?php
  $number = 23;
  echo "Our number is $number. <br>";

  $number = 42;
  echo "Our number is now $number. <br>";
?>

 

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif