Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7411913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:27:36+00:00 2026-05-29T06:27:36+00:00

I would like to preg_replace some text but only if it’s not commented. Here’s

  • 0

I would like to preg_replace some text but only if it’s not commented. Here’s an example of a given text:

// delete_me('First');
delete_me('Second');
/* delete_me('Third'); */
delete_me('Fourth'); // Some comment behind a command.
/* Tiny bit of comment */ delete_me('Fifth');

Now in this example I would only want the second, fourth and fifth row to be replaced. I want to replace the parameter with a new one. All the text is in one big string, separated by newlines.

I do have some preg_replaces to remove comment sections, but since I don’t want to delete them it’s not much use, but perhaps it helps someone helping me.

$text = preg_replace("/\/\/(.*)/", $replace, $text);
$text = preg_replace('!/\*(.*)\*/!s', $replace, $text);

Can anyone help me to replace the given parameters on lines that are not PHP commented? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:27:37+00:00Added an answer on May 29, 2026 at 6:27 am

    First split the text into chunks of comment and non-comment, then only alter the non-comment pieces, and finally glue them together:

    $in = "// delete_me('First');
    delete_me('Second');
    /* delete_me('Third'); */
    delete_me('Fourth'); // Some comment behind a command.
    /* Tiny bit of comment */ delete_me('Fifth');\n";
    
    $split = preg_split("#(//[^\n]*\n|/\\*.*?\\*/)#s",$in,-1,PREG_SPLIT_DELIM_CAPTURE);
    
    foreach ( $split as $i=>$chunk )
    {
        if ( $i%2==0 )
        {
            $split[$i] = preg_replace("/'.*?'/","'newparam'",$chunk);
        }
    }
    
    echo implode($split);
    

    output:

    // delete_me('First');
    delete_me('newparam');
    /* delete_me('Third'); */
    delete_me('newparam'); // Some comment behind a command.
    /* Tiny bit of comment */ delete_me('newparam');
    

    The trick here is that the pattern supplied to preg_split matches the comment blocks, so you get an array of even/odd pieces of code/comment.

    caveat This will of course break when you would put /* in a string literal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to parse some text into an array: My text looks like
I have some text contain html tags, I would like to replace all links
Hello I would like to use preg_match in PHP to parse the Desired text
Would like to make anapplication in Java that will not automatically parse parameters used
I would like to add links around image tags with preg_replace(). Before: <img href=
im new to regular expressions and would like to decipher this. return preg_replace(/[<>]/, '_',
I have some text, something like this: Paragraphs of text (SOME KNOWN TEXT)Unknown Text(SOME
Given a literal string such as: Hello\n\n\n\n\n\n\n\n\n\n\n\nWorld I would like to reduce the repeated
I would like to use some kind of BB code for a php application:
I would like to catch and replace a regex in a variable but I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.