Need to skip multiline charachters and single line character

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
glitteringsound
Forum Newbie
Posts: 1
Joined: Sun Jan 24, 2010 5:12 am

Need to skip multiline charachters and single line character

Post 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
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Need to skip multiline charachters and single line character

Post 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!
Post Reply