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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:10:25+00:00 2026-06-12T18:10:25+00:00

I am trying to convert a multi-line string, to insert line breaks \n after

  • 0

I am trying to convert a multi-line string, to insert line breaks \n after two final closing parenthesis occur in series, ie: )) becomes ))\n.

There is also likely to be a ‘)’ just prior to the ‘))’, effectively creating ‘)))’.

These two or three parenthesis may or may not be “spread out” by indeterminate lengths of whitespace, eg )), ) ), ))), ) ) ), )) ), ) )) and so on.

I’ve tried the following:

//Example message
$message = '(item (name 286) (Index 31) (Image "item001") (class money coin) (code 4 110 0) (country 2) (plural 1) (buy 0))
(item(name 7904)(Index 7904) (specialty (Dex 10(defense 55)(hp 3500)(dodge 71)          ))

(item(name 7905)(Index 7905)(country 2) 
(level 80)(specialty(hp 3400)               )                               )

(item(name 7906)(Index 7906)(level 80)      (specialty(Str 10))                             ) ';

// Converts all lines into one line    
$message =  preg_replace("/[\r\n]*/","",$message); 

// Replace '))' with '))\n' - doesn't work.
$message =  preg_replace("/[)s+)]s*/","\n",$message); 

$InititemLines = explode("\n", $message);
for ($line = 0; $line < count($InititemLines); $line++) {
    echo "Line #<b>{$line}</b> : " . $InititemLines[$line] . "<br />\n";
}

To convert all lines into one, I used:

$message = preg_replace("/[\r\n]*/","",$message);

Then, to replace )) with ))\n, I tried the following (but it doesn’t work):

$message = preg_replace("/[)s+)]s*/","))\n",$message);

I want the output to be like this:

Line #0: (item (name 286) (Index 31) (Image "item001") (class money coin) (code 4 11 0 0) (country 2) (plural 1) (buy 0))
Line #1: (item(name 7904)(Index 7904) (specialty    (Dex 10)(defense 55)(hp 3500)(dodge 71)         ))
Line #2: (item(name 7905)(Index 7905)(country 2)(level 80)(specialty(hp 3400)       )           )
Line #3: (item(name 7906)(Index 7906)(level 80)     (specialty(Str 10))     )
  • 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-12T18:10:26+00:00Added an answer on June 12, 2026 at 6:10 pm

    This will replace the “))” at the end of ALL lines, in the case of line ending in ))) or )):

    $message = Preg_replace( "\)?(\s*\)\s*\))", "$1\n", $message );

    This regex means

    • find an optional single closing parenthesis ‘)’. We escape it as ‘)’ as the parenthesis has special meanings in regex. It’s optional because of the trailing ‘?’.
    • followed by 0 or more space characters denoted as ‘\s’. 0 or more denoted by ‘*’,
    • followed by another ‘)’
    • followed by another 0 or more spaces,
    • followed by another ‘)’
    • Then we surround \s*\)\s*\) with a pair of ‘(‘ and ‘)’ meaning “group this section, so we can reference it later”. We do this so we can replace it with ))\n.

    And then a more elegant solution might be (depending on your requirements…), to subsequently also strip any excess remaining spaces from before every ‘)’:

    $message = preg_replace("(\)\s*)", "\)", $message);

    This regex means

    • find an operning ‘)’,
    • followed by 0 or more spaces
    • Grouped, so we can replace.

    (In your example, I believe this will strip all the excess whitespace, while leaving the spaces in your strings alone).

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

Sidebar

Related Questions

Trying to convert a string of binary inputs into a vector of ints. I'd
Trying to convert int arrays to string arrays in numpy In [66]: a=array([0,33,4444522]) In
I am trying to convert a multi-level array to a table. However my code
I'm trying to use ghostscript to convert a .ps file to a series of
I'm new to the Entity Framework and I'm trying to convert a multi table
I am trying to convert a multidimensional array into a string with a particular
I am trying to convert an existing Android, SQLite database to a multi-platform, SQLite
I am trying to convert a string into a wchar_t string to use it
I'm trying to convert a number of single tiffs to a multi-page tiff in
I am trying to convert the keys of a multi-dimensional array from camelCase or

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.