Ben
Ben

Reputation: 318

Remove text between comments

In order to send query to execute with JDBC I have to remove texte between /* and */

/*==============================================================**/
/* Description : ... */
/* Author   : rgosse                                                  */
/* Date de création du script : 25/04/2013                    */
/*==============================================================*/

/*==============================================================*/
/* PISTE D'AUDIT                                                */
/*==============================================================*/
declare @username varchar(25)
declare @dateNow datetime
declare @contact_id numeric
...

can I do it with preg_replace ?

someting like

preg_replace("/[/*][^*\/]+\[*/]/","", $sql);

Upvotes: 0

Views: 95

Answers (3)

AbsoluteƵERØ
AbsoluteƵERØ

Reputation: 7870

To replace the text between the comments we need to match the beginning and ending comment and then replace the elements between those. Since * is a quanitifer we'll need to escape it \* so its natural behavior doesn't throw an error in PHP.

Note: In the following, I'm assuming balanced comment marks.

$code = "
/*==============================================================**/
/* Description : ... */
/* Author   : rgosse                                                  */
/* Date de création du script : 25/04/2013                    */
/*==============================================================*/

/*==============================================================*/
/* PISTE D'AUDIT                                                */
/*==============================================================*/
declare @username varchar(25)
declare @dateNow datetime
declare @contact_id numeric
";

$pattern = "!(/\*).+(\*/)!";
$replacement = '/**/';

$newCode = preg_replace($pattern,$replacement,$code);

echo $newCode;

Output:

/**/
/**/
/**/
/**/
/**/

/**/
/**/
/**/
declare @username varchar(25)
declare @dateNow datetime
declare @contact_id numeric

If you want to replace all comments entirely, make the following change:

$replacement = '';

Upvotes: 0

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89547

You can use this pattern

$code = preg_replace('~/\*(?>[^*]++|\*++(?!/))*\*++/~', '', $code);

Details:

~              # delimiter (instead of /, avoid to escape all the / in the pattern)
/              # literal /
\*             # literal * (must be escaped since it's a special character)
(?>            # open a non capturing group (atomic)
    [^*]++     # all characters except * one or more times
  |            # OR
    \*++(?!/)  # * one or more times not followed by /
)*             # repeat the group zero or more times (here * is a quantifier)
\*++           # * one or more times
/              # literal /
~              # pattern delimiter

Upvotes: 2

DevZer0
DevZer0

Reputation: 13525

you need to escape chars like / and * because they have special meaning, you only have done it in one place. The code below should do the trick

 preg_replace("^/\/\*[^*\/]+\*\/$/","", $sql);

Upvotes: 0

Related Questions