?
Solved

PHP Regular Expression for formatting comments...

Posted on 2011-09-10
3
Medium Priority
?
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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 111

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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

800 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