Page 1 of 1

Need to skip multiline charachters and single line character

Posted: Sun Jan 24, 2010 5:14 am
by glitteringsound
Hello,
Can any body tell me i need to skip the whole text which is commented (whether multiline or single line) and match the next text with simple regexs.

e.g

/*
This is sample text_1 in multi line comments
This is sample text_2 in multi line comments
*/
Text after comments

Now i only want to match string "Text after comments" and regex whould skip whole commented text and only give me output as "Text after comments"

Regards
Muhammad Usman Khalil

Edit/Delete Message

Re: Need to skip multiline charachters and single line character

Posted: Sun Jan 24, 2010 6:31 am
by Apollo

Code: Select all

function RemoveComments( $s )
{
  return preg_replace('#/\*.*?\*/#s','',$s);
}
Trickery involved:
  • \* instead of * because * is a special regexp char
  • .*? means lazy instead of greedy (otherwise it would cut everything between the first and last comment, if multiple)
  • the 's' modifier to ignore linebreaks
Have fun!