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 8343325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:01:24+00:00 2026-06-09T06:01:24+00:00

match a // in a line of array and replace // with blank space

  • 0

match a // in a line of array and replace // with blank space if its not inside " ".
For example I have this line:

//this is a test
"this is a // test"

the output should be:


"this is a // test"

it will ignore // inside ""

for now I’ve come up with this regex:

$string[$i] = preg_replace('#(?<!")//.*(?!")#',' ',$string[$i]);

but this does’t work if the // is in the middle or last part of a line.

  • 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-06-09T06:01:26+00:00Added an answer on June 9, 2026 at 6:01 am

    Since you don’t have to worry about quotes that span multiple lines, this makes your job significantly easier.

    One approach would be to go through the input line-by-line and explode() each line using " as the delimiter:

    $processed = '';
    
    /* Split the input into an array with one (non-empty) line per element.
     *
     * Note that this also allows us to consolidate and normalize line endings
     *  in $processed.
     */
    foreach( preg_split("/[\r\n]+/", $input) as $line )
    {
      $split = explode('"', $line);
    
      /* Even-numbered indices inside $split are outside quotes. */
      $count = count($split);
      for( $i = 0; $i < $count; $i += 2 )
      {
        $pos = strpos($split[$i], '//');
        if( $pos !== false )
        {
          /* We have detected '//' outside of quotes.  Discard the rest of the line. */
          if( $i > 0 )
          {
            /* If $i > 0, then we have some quoted text to put back. */
            $processed .= implode('"', array_slice($split, 0, $i)) . '"';
          }
    
          /* Add all the text in the current token up until the '//'. */
          $processed .= substr($split[$i], 0, $pos);
    
          /* Go to the next line. */
          $processed .= PHP_EOL;
          continue 2;
        }
      }
    
      /* If we get to this point, we detected no '//' sequences outside of quotes. */
      $processed .= $line . PHP_EOL;
    }
    echo $processed;
    

    Using the following test string:

    <?php
    $input = <<<END
    //this is a test
    "this is a // test"
    "Find me some horsemen!" // said the king, or "king // jester" as I like to call him.
    "I am walking toward a bright light." "This is a // test" // "Who are you?"
    END;
    

    We get the following output:

    
    "this is a // test"
    "Find me some horsemen!" 
    "I am walking toward a bright light." "This is a // test" 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following line: hshd household 8/29/2007 LB I want to match anything
match.matches() returns false. This is odd, because if I take this regex and test
Which php array function should be used to match a string to the first
I'm not sure how to approach this, i'm trying to match this long string
I have these strings in an array and I want to iterate this array
I am trying to match (a) and replace (b) the following occurrences: array[0] ->
I have made this line of code to make the words, that is searced
End of line anchor $ match even there is extra trailing \n in matched
I have the following line of code in javascript: (Math.random() + ) * 1000000000000000000
Match match = Regex.Match(555-5555555, ^(\\d{3}\\-?\\d{3}\\-?\\d{4})$, RegexOptions.None); it[Should match] = () => match.Success.should_be_true(); The above

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.