0

I would like to use preg_replace to replace "* @license until */" with "testing".
How can I do it?
My text is the one below:

/*
 * @copyright  
 * @license  
 *
 */

I hope everyone have understand my question correctly.

2 Answers 2

2

Here is one regex that does what you want (run it in multi-line mode)

^\s*\*\s*@license(?:(?!\s*\*/)[\s\S])+

It matches the part that is striked:

/*
 * @copyright  
 * @license  
 *
 */

Explanation:

^              ~ start-of-string
\s*            ~ any number of white space
\*             ~ a literal star
\s*            ~ any number of white space
@license       ~ the string "@license"
(?:            ~ non-capturing group
  (?!          ~   negative look ahead (a position not followed by...):
    \s*        ~     any number of white space
    \*         ~     a literal star
    /          ~     a slash
  )            ~   end lookahead (this makes it stop before the end-of-comment)
  [\s\S]       ~   match any single character
)+             ~ end group, repeat as often as possible

Note that the regex must still be escaped according to PHP string rules and according to preg_replace() rules.

EDIT: If you feel like it - to make absolutely sure that there really is an end-of-comment marker following the matched text, the regex can be expanded like this:

^\s*\*\s*@license(?:(?!\s*\*/)[\s\S])+(?=\s*\*/)
                                      ↑           positve look ahead for 
                                      +-----------an end-of-comment marker
Sign up to request clarification or add additional context in comments.

Comments

0

Well, it's not too hard. All you need to do is use the s modifier (PCRE_DOT_ALL, which makes a . in the regex match new lines):

$regex = '#\\*\\s*@license.*?\\*/'#s';
$string = preg_replace($regex, '*/', $string);

That should work for you (note, untested)...

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.