2011-02-17 07:41


I am about writing own simple syntax highlighter in PHP. I've done basic algorithm based on regular expressions and string replacement, but what I really don't know is way how to disable replacing keywords which are commented.

For example:

 * Some class
 * @property-read $foo
 class Test
     private $foo;

     public function __construct()

Now my solution simply highlight defined keywords (like class or variables) but also those which are commented.

Any solution for this problem?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • douwan7382 douwan7382 10年前

    Why not borrow lessons from how vi or vim already does this? long back I remember for some custom tag based language we developed, we wanted syntax highlighting in VI and VIM , that is when we changed few .vi sort of configuration files where we mentioned, all the meta data like which color to what kind of tag, what are tags possible etc.

    Looking more into how vi or vim or any text editor does this might be more helpful!

    点赞 评论 复制链接分享
  • doswy02440 doswy02440 10年前

    Why not use PHP's tokenizer to do the job for you? That way, your syntax highlighter will parse the PHP code the exact same way the Zend Engine does, which is probably going to give you a lot better results than a regular expression.

    点赞 评论 复制链接分享
  • dongyuan9109 dongyuan9109 10年前

    You could exclude the commented lines by this logic:

    if line starts with /** disable highlight
    if next line starts with * do nothing and check next line
    if line starts with */ reenable highlight

    Just a quick guess and can be defined more precise, but should work as a logic.

    点赞 评论 复制链接分享