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 6069935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:52:37+00:00 2026-05-23T09:52:37+00:00

I was creating a Syntax Highlighter in PHP but I was failed! You see

  • 0

I was creating a Syntax Highlighter in PHP but I was failed! You see when I was creating script comments (//) Syntax Highlighting (gray) , I was facing some problems. So I just created a shortened version of my Syntax Highlighting Function to show you all my problem. See whenever a PHP variable ,i.e., $example, is inserted in between the comment it doesn’t get grayed as it should be according to my Syntax Highlighter. You see I’m using preg_replace() to achieve this. But the regex of it which I’m using currently doesn’t seem to be right. I tried out almost everything that I know about it, but it doesn’t work. See the demo code below.

Problem Demo Code

<?php
  $str = '
  <?php
    //This is a php comment $test and resulted bad!
    $text_cool++;
  ?>
  ';
  $result = str_replace(array('<','>','/'),array('[',']','%%'),$str);
  $result = preg_replace("/%%%%(.*?)(?=(\n))/","<span style=\"color:gray;\">$0</span>",$result);
  $result = preg_replace("/(?<!\"|'|%%%%\w\s\t)[\$](?!\()(.*?)(?=(\W))/","<span style=\"color:green;\">$0</span>",$result);
  $result = str_replace(array('[',']','%%'),array('&lt;','&gt;','/'),$result);
  $resultArray = explode("\n",$result);
  foreach ($resultArray as $i) {
    echo $i.'</br>';
  }
?>

Problem Demo Screen

enter image description here

So you see the result I want is that $test in the comment string of the ‘Demo Screen’ above should also be colored as gray!(See below.)

enter image description here

Can anyone help me solve this problem?

I'm Aware of highlight_string() function!

THANKS IN ADVANCE!

  • 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-05-23T09:52:37+00:00Added an answer on May 23, 2026 at 9:52 am

    I agree, that you should use existing, parsers. Every ide has a php parser, and many people have written more of them.

    That said, I do think it is worth the mental exercise. So, you can replace:

    $result = preg_replace("/(?<!\"|')[\$](?!\()(.*?)(?=(\W))/","<span style=\"color:green;\">$0</span>",$result);
    

    with

    //regular expression.:
    //#([^(%%%%|\"|')]*)([\$](?!\()(.*?)(?=(\W)))#
    
    //replacement text:
    //$1<span style=\"color:green;\">$2</span>
    
    $result = preg_replace("#([^(%%%%|\"|')]*)([\$](?!\()(.*?)(?=(\W)))#","$1<span style=\"color:green;\">$2</span>",$result);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some advice on creating a syntax highligher for java application using swing.
Creating a Lisp syntax like DSL - very small specific application - but very
I am creating a in browser HTML editor. I am using the syntax highlighter
Does anyone know if there is any tutorial on creating syntax highlighter for custom
I am new to php syntax and am looking for advice on creating the
I'm not sure if this syntax is Informix-specific, but I was having trouble creating
I have tried creating a syntax highlighter for a custom file definition. I get
I'm creating a series of builders to clean up the syntax which creates domain
(creating a separate question after comments on this: Javascript redeclared global variable overrides old
I am creating an extension that will launch an external script based on highlighted

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.