0

I have this:

$text = 'This is some text /*Comment 1 */ . Some more text{ This is to let you know that /* this is a comment*/. A comment /*this one is */ can be anything }. So the next thing { This is to let you know that /*  this is a comment*/. A comment /*this one is */ can be anything } is another topic. /*Final comment*/';

Need this:

$text = 'This is some text /*Comment 1 */ . Some more text{ This is to let you know that . A comment  can be anything }. So the next thing { This is to let you know that . A comment  can be anything } is another topic. /*Final comment*/';

Tried this:

$text = preg_replace("/\/\*.*?\*\//", "", $text);

The problem is that what I have tried, is removing all the comments. I just want the comments appearing within { } to be removed. How to do this?

3
  • What if you have something like { /* } */ }? Commented Apr 1, 2012 at 5:40
  • Haven't thought of it. Such kind of data is unexpected. What would be a possible solution in such a case? Commented Apr 1, 2012 at 6:44
  • It’s all up to you. But it’s a case that you should consider when defining such a language. Commented Apr 1, 2012 at 6:47

2 Answers 2

2

You can use the following regular expression to tokenize the string:

$tokens = preg_split('~(/\*.*?\*/|[{}])~s', $str, -1, PREG_SPLIT_DELIM_CAPTURE);

Then iterate the tokens to find opening { and the comments inside them:

$level = 0;
for ($i=1, $n=count($tokens); $i<$n; $i+=2) {  // iterate only the special tokens
    $token = &$tokens[$i];
    switch ($token) {
    case '{':
        $level++;
        break;
    case '}':
        if ($level < 1) {
            echo 'parse error: unexpected "}"';
            break 2;
       }
       $level--;
       break;
   default:  // since we only have four different tokens, this must be a comment
       if ($level > 0) {
           unset($tokens[$i]);
       }
       break;
   }
}
if ($level > 0) {
    echo 'parse error: expecting "}"';
} else {
    $str = implode('', $tokens);
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Because even this is a working solution. Your level of understanding of how this works is a lot more than me. And because I cant accept 2 posts as an answer, I am voting this up.
0

This is probably the safest way:

<?php

$text = 'This is some text /*Comment 1 */ . Some more text{ This is to let you know that /* this is a comment*/. A comment /*this one is */ can be anything }. So the next thing { This is to let you know that /*  this is a comment*/. A comment /*this one is */ can be anything } is another topic. /*Final comment*/';

$text = preg_replace_callback('#\{[^}]+\}#msi', 'remove_comments', $text);

var_dump($text);

function remove_comments($text) {
    return preg_replace('#/\*.*?\*/#msi', '', $text[0]);
}

?>

It searches for {} then removes the comments inside them. This will remove multiple comments in a {}.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.