Solved

PHP Regular Expression for formatting comments...

Posted on 2011-09-10
3
297 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 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 110

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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 …

688 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