Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP Regular Expression for formatting comments...

Posted on 2011-09-10
3
Medium Priority
?
299 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

610 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