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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:57:53+00:00 2026-06-06T21:57:53+00:00

I want to split a text into an array where the delimiter is one

  • 0

I want to split a text into an array where the delimiter is one or more of “\n”, and then put the content of the array as elements in a unorder html-list. When I do this using preg_split when there is a $1 in the subject string I get a weird result. Just looking at the array resulting from the splitting the result is fine and the $1 seems not to have caused any problem, but when I loop over the array and make it into a html list it creates a different result then expected(see example bellow)

Like if this was the subject string:

“First line

Second line $1

Third line”

It should become:

  • First line
  • Second line $1
  • Third line

But it becomes:

  • First line
  • Second lineFirst line
    Second line $1
    Third line
  • Third line

Does anyone know why this happens?
Is $1 some kind of special html or php character with a reserved meaning?

This is the code I’ve written:

        $listElements = preg_split('/[\n]+/',$subject);
        $output = '<ul>';
        foreach ( $listElements as $val ) {
          $output .= '<li>' . $val . '</li>';
        }
        $output .= '</ul>';
  • 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-06T21:57:56+00:00Added an answer on June 6, 2026 at 9:57 pm

    Using dollar symbols inside PHP strings has a weird effect to those who aren’t familiar with it.

    $foo = "BAR";
    $example = "Hello, World! $foo $foo black sheep";
    echo $example;
    

    The above example echoes: Hello, World! BAR BAR black sheep. This is because the variable is expanded within the string.

    A simple way of stopping this behaviour is by using single quotes: Changing the above example to 'Hello, World! $foo $foo black sheep'; will echo this: Hello, World! $foo $foo black sheep, and the dollars will be left in place.

    Another way of stopping variables expanding inside double-quoted strings is by escaping them with a backslash:

    $example = "Hello, World! \$foo \$foo black sheep";
    

    will echo:

    Hello, World! $foo $foo black sheep


    In your example, I would assume that $val has a dollar symbol in it, which needs escaping.

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

Sidebar

Related Questions

I'm taking some text and want to split it into an array. My goal
I want to put some text from a text file into an array, but
I want to split a file containg HTTP response into two files: one containing
Is there any inbuilt way in C# to split a text into an array
I want to split text into words, to count the number of words. This
I want to split a long text into smaller chunks, that will act as
what I want to do is to split the text of textarea on cursor
I want to split a file(suppose a mp3) into four parts.I have tried this
I want to split a series of divs vertically: My HTML might look like
I want to split and transpose a multiline string from one DataGridView(dgvObs) to separate

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.