Jump to content

Recommended Posts

Here's what I want to do, search through posted code and find the 3 most common types of comments,

 

/* */

//

and #

 

// and # are single line so I thought something like this would work

 

/** match //text */
$code = preg_replace("/\/\/(.*?)(.*?)/", " [color=green][b]//\\1[/b][/color] ", $code);

/** match #text */
$code = preg_replace("/\#(.*?)/", " [color=lightgreen][b]#\\1[/b][/color] ", $code);

 

However it's only grabbing the # and // leaving everything to the right out

 

And /* comments can run until they hit a */, which makes the multi line, I tried this but it also doesn't work

 

$code = preg_replace("/\/\*(.*?)\/\*/s", " [color=green][b]/*\\1*/[/b][/color] ", $code); 

 

anyone know what I need to do to make these work?

Link to comment
https://forums.phpfreaks.com/topic/130612-attempting-to-match-code-style-comments/
Share on other sites

You might want to consider a pre-made syntax highlighter for PHP (many are available), the issue with any regex pattern for this operation would be how do you keep from matching your target strings within string declarations etc.

 

/\*(??!\*/).)*\*/|(?://|#)[^\r]*

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.