Solved

PHP Regular Expression for formatting comments...

Posted on 2011-09-10
3
291 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 74

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 108

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now