Solved

PHP Regular Expression for formatting comments...

Posted on 2011-09-10
3
294 Views
Last Modified: 2012-05-12
I'm trying to clean up some comments in a bunch of pages, and I want them to be in this format:

/* this is
 * a comment
 * and I like it with a space in front of the asteriks
 *
 */

But most of them seem to be like this:

/* this is a comment
foo bar
comment text
*/

Can I use regex to fix them? And if so what would it be?
0
Comment
Question by:interclubs
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 36517722
Try this out:
<?php
         $source = <<<HERE
/* this is a comment
foo bar
comment text
*/
HERE;

         echo "<pre>";

         while (preg_match('#(/\*(?:[^*]|\*(?!/))*^)\s*(?![\s*])#m', $source, $match)) {
             $source = str_replace($match[0], $match[1] . " * ", $source);
         }

         echo $source;

         echo "</pre>";			
?>

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36517739
kaufmed is probably right (usually is) but I am curious -- how many such files are involved?
0
 

Author Comment

by:interclubs
ID: 36517788
probably about 200 hundred....
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question