Prismatic Posted October 29, 2008 Share Posted October 29, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/130612-attempting-to-match-code-style-comments/ Share on other sites More sharing options...
ddrudik Posted October 29, 2008 Share Posted October 29, 2008 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]* Quote Link to comment https://forums.phpfreaks.com/topic/130612-attempting-to-match-code-style-comments/#findComment-677651 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.