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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:46:13+00:00 2026-05-28T04:46:13+00:00

My database contains records that have: 1) First line delimited with double \n\n and

  • 0

My database contains records that have:

1) First line delimited with double \n\n and the rest delimited with \n:

Introduction 

Line 1
Line 2

2) Everything delimited with \n:

Line 1
Line 2
Line 3

3) Nothing delimited:

Introduction 

My goal is to add “>>” before each line that’s delimited with a single \n. The spaghetti dinner that I cooked (and it works) looks as follows:

list($intro, $details) = split("\n\n", $dbInput);
if ($details) {
    $temp = split("\n", $details);
    array_walk($temp, create_function('&$v,$k', '$v = ">> $v";'));
    $dbOutput = "$intro \n\n ".join("\n", $temp);
} else {
    $temp = split("\n", $intro);
    if (count($temp) > 1) {
        array_walk($temp, create_function('&$v,$k', '$v = ">> $v";'));
        $dbOutput = join("\n", $temp);
    } else $dbOutput = $temp[0];
}

An example of what I want to achieve:

Introduction

>> Line 1
>> Line 2
>> Line 3

or

>> Line 1
>> Line 2
>> Line 3

or

Introduction

QUESTIONS: How would I optimize this code to combine array_walk with split and join in the same statement somehow along the following lines:

$a = join("\n" , array_walk(split("\n", $a), create_function(('&$v,$k', '$v = ">> $v";')) ) );
  • 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-28T04:46:14+00:00Added an answer on May 28, 2026 at 4:46 am

    My personal method would be to use preg_prelace to replace lines terminating in \n (and not \n\n with >> at the front.

    My guess is you don’t want the

    Introduction
    

    To have the >> in front?

    In that case:

    $pattern = '/([^\n]+)(?=\n(?!\n))|(?<=(?<!\n)\n)([^\n]+)/';
    $replacement = '>>$1$2';
    $subject = 'Introduction 
    
    Line 1
    Line 2
    Line 3';
    
    echo preg_replace ($pattern, $replacement, $subject);
    
    /* echoes:
    Introduction
    
    >>Line 1
    >>Line 2
    >>Line 3
    */
    

    Explanation of regex:

    ([^\n]+)(?=\n(?!\n))   # match a line followed by \n but not a \n\n
    |                      # OR
    (?<=(?<!\n)\n)([^\n]+) # match a line preceded by \n but not a \n\n
    

    The first bit captures Line1 and Line2, but not Line3, as this is terminated by end-of-string and not a \n. The second bit of regex captures Line3.

    They also both exclude a single-line string like Introduction.

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

Sidebar

Related Questions

I have a database that contains transaction records. Each record belongs to a chain
I have a database that has around 10k records and some of them contain
I have a table in my MySQL database which contains records of activity. It
I have a database that contains a two tables, where these table have a
I have a DataGridView that I want users to be able to add records
I have a table in my MySQL database containing 200K records. Each record contains
My database contains both questions and answers. The questions have an ID (intQAID) and
I have a database which contains the link to audio files. I am trying
i have a database which contains an arabic tables , i can read it
I have a question about generic list. Assume that I have a database which

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.