блоки комментариев php preg_replace - PullRequest
1 голос
/ 22 марта 2011

образец такой:

/* comment [comment goes here] */
/* comment please do not delete the lines below */
[I am a special line so I should not be changed ]
/* comment please do not delete the line above */

Я хотел бы удалить блоки комментариев, но вместо поиска /**/ я бы хотел, чтобы комментарий был /* comment [] */, где [] - этофактический комментарий.

Это позволяет избежать любого текста, который должен содержать комментарии.

, так что вот условия комментария

  1. начинается с = >> /* comment
  2. с последующим = >> что угодно
  3. с последующим = >> */

Ответы [ 2 ]

3 голосов
/ 22 марта 2011

Это удаляет блоки комментариев:

preg_replace('%/\*\s+comment\s+.*?\*/%s', '', $string)

И это также избавляет от устаревших пробелов:

preg_replace('%/\s*\*\s+comment\s+.*?\*/\s*%s', '', $string)

Вот тестовый скрипт:

#!/usr/bin/php
<?php

$string = <<<EOS
/* comment [comment goes here] */
/* comment please do not delete the lines below */
[I am a special line so I should not be changed ]
/* comment please do not delete the line above */
EOS;

print $string;
print "\n---\n";
print preg_replace('%/\*\s+comment\s+.*?\*/%s', '', $string);
print "\n---\n";
print preg_replace('%/\s*\*\s+comment\s+.*?\*/\s*%s', '', $string);

?>

Вывод с PHP 5.3.4:

/* comment [comment goes here] */
/* comment please do not delete the lines below */
[I am a special line so I should not be changed ]
/* comment please do not delete the line above */
---


[I am a special line so I should not be changed ]

---
[I am a special line so I should not be changed ]
0 голосов
/ 22 марта 2011

Кажется, чтобы сделать работу:)

preg_replace("(\\/\\*[\s]*?comment[\\d\\D]*?[\s]*?\\*\\/)",'',$str)

Как я узнал?

ну, этот веб-сайт просто потрясающий :)

http://txt2re.com/index.php3

...