简体   繁体   中英

Php and string analysis: how to remove comments recursively

I have a string with comments in it C-style like test/* comment */ . I would like to remove them, and I would like to get this as a result: test

I've done something working, but now I would like to be able to have comment into comment, and ignore "nested comment". For example I would like to make a function that removes comments in strings like test/* co/* comment */mmen/* comment */t */ and would still return this as a result: test .

How would you do?

这是一个删除多行注释的正则表达式:

$text = preg_replace('%/\*.*?\*/%', null, $text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM