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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:34:22+00:00 2026-06-16T15:34:22+00:00

Given, In a input form user can separate some specific names by new line

  • 0

Given, In a input form user can separate some specific names by new line and i am saving those names in an array. Then I print those names and at last say something like “thank you for the names”.

         $var = "name1 



name2


        ";
    $your_array = explode("\n", $var);


        for($i=0; $i<(sizeof($your_array));$i++) {
          echo ($your_array[$i]);  
        }
echo "Thank you for the names"

But the problem is if someone enter more than one newline before or after a name then the next name show after some distance like below

    name1



    name2


Thank you for the names

How can escape this and output as below

name1
name2
Thank you for the names

I tried to use array_filter() but it don’t work here.

Update:

If someone input
$var = "name1\nname2\n\n\n


           name3                     


name4";

output should be like

name1
name2
               name3
name4

But all the answers show like

name1
name2
name3
name4
  • 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-16T15:34:24+00:00Added an answer on June 16, 2026 at 3:34 pm

    Let’s use a foreach loop for the sake of simplicity…

    1) We need to iterate through the new array and trim the names so we lose all whitespace.

    2) We echo out the name, if and only if the length of the name is 1 or more.

    3) We add on a <br> tag to make each name appear on a new line.

    <?php
    
    $var = "name1\nname2\n\n\n";
    
    $your_array = explode("\n", $var);
    
    foreach($your_array as $name)
    {
        if(preg_match("/[a-zA-Z0-9]{1,}/i", $name)) {
            $name = trim($name);
            echo (strlen($name) > 1) ? sprintf('%s<br>', $name) : '';
        }
    }
    
    echo "Thank you for the names";
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following HTML form: <form id=myform> Company: <input type=text name=Company value=ACME, INC./> First
Given this HTML: <form id=formx> <input type=text value= /> </form> And this JavaScript: $(function()
Is it possible to read alphanumeric characters only from the given input line ignoring
You are given as input an unsorted array of n distinct numbers, where n
I have a form that allows uploads of images. The user can select which
Ugh! Any ideas? Have a form which tracks user activity for given time intervals.
I have a user input form here: http://www.7bks.com/create (Google login required) When you first
Given: WPF 4.0 desktop-based application. Basic input form with two TextBox fields and submit
I have a sine wave whose parameters I can determine (they are user-input). It's
I have a form where user can select search criteria. The criterias are say:

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.