Php text in one line

How to Break up a Text Into Separate Lines In PHP?

New here? Like SchoolsOfWeb on Facebook to stay up to date with new posts.

Problem:

You have a long string that consists of some lines (see the following code). Each line consists of information of each employee and lines are separated by new line characters. Now, we would like to separate each employee information in different variable so that we can use it according to our needs.

Use explode() function in php.

Output:
———————————————————————————————
Name: Keith B. Covey Designation: Project Co-ordinator Height: 187 centimeters.
———————————————————————————————
Name: John C. Evans Designation: Cheif Operating Officer Height: 179 centimeters.
———————————————————————————————
Name: Ernest K. Peterson Designation: Senior Marketing Executive Height: 180 centimeters.
———————————————————————————————
Name: Daniel M. Beal Designation: Human Resource Manager Height: 182 centimeters.
———————————————————————————————

Line 1 to 6 Variable $employees holds a long text which consists of information of all employees.
Line 7 The explode function splits the content of variable $employees by PHP_EOL which is a PHP constant that indicates end of a line. In the $employees variable, after each employee information, the line ends and new employee information starts in the nest line. After splitting, the explode function returns an array. We define that array as $employee_arr. Each element of the array $employee_arr stores information of each employee.
Line 8 to 11 The foreach loop prints each employee information as separate line.
Читайте также:  Javascript check if argument is function

explode() function at a glance

explode() function splits up a string into an array.

explode(separator, string, limit)

  • If the limit is a positive number, the returned array will contain a maximum of limit element with the last element containing the rest of string.
  • If the limit is zero, it is equivalent to 1.
  • If the limit is negative, it removes the last limit elements and returns the array.

Example 1: How to retrieve minute from 10:30:12

Line 3 >The explode function splitting up the variable $time by “:” generating 10, 30, and 12. Then, it stored those values in $time_arr
Line 4 As minute is in the second element(10:30:12), so, it is stored in the second element of the $time_arr array. That is $time_arr[1].
Line 5 Printing the minute

Example 2: Use of optional parameter

"; echo $language; > echo " 
-------------"; $languages_arr = explode(" ", $languages, 0); foreach ($languages_arr as $language)< echo "
"; echo $language; > echo "
-------------"; $languages_arr = explode(" ", $languages, -2); foreach ($languages_arr as $language)< echo "
"; echo $language; > ?>

Output:
PHP
MySQL
JavaScript
jQuery CSS HTML
————-
PHP MySQL JavaScript jQuery CSS HTML
————-
PHP
MySQL
JavaScript
jQuery

Line 4 Assigning 4 in the third optional parameter, the explode function returns an array with 4 elements, the fourth element(jQuery) contains the remaining part(CSS HTML) of the string
Line 12 In the third parameter 0 is equivalent to 1. Assigning 0, the explode function returns an array with one element (PHP) and add the remaining part of the string( MySQL JavaScript jQuery CSS HTML) with this.
Line 20 Assigning -2 in the third optional parameter, the explode function returns an array with all the elements except the last 2 elements (CSS HTML).
Читайте также:  Php get apache env

Courses

Источник

Attempting to concatenate a text without any line breaks

To achieve my desired output of a bunkhouse with a front bedroom, kitchen island, loft, outdoor kitchen, and 4 slide-outs, I need help finding a solution. One option is to use nl2br for regular line breaks, while for a UL list, another solution involves realizing that the current solution may not be working as expected due to inserting html line break elements instead of newline characters.

«m trying to echo a text into one line with no line breaks

In an attempt to eliminate line breaks, I attempted to use nl2br and remove all instances of /n or /r. My desired output is a single line of text without any breaks.

Attempting to condense my text into a single line.

«I»m trying to write my text

Apart from the requirement of replacing nl2br with a space instead of an empty string, your code is functional. It is worth noting that you can enhance the efficiency of preg_replace by utilising \ \ \ \ \ \R\ \ \ \ , which can recognise any unicode newline sequence.

$mytext = "I'm trying to write my text\r in one line"; echo preg_replace('/\R/',' ', $mytext); echo PHP_EOL; echo str_replace(array("\r\n", "\r","\n"), " ", $mytext); 
I'm trying to write my text in one line I'm trying to write my text in one line 

To ensure that it stays on a single line in the HTML, replace it with non-breaking spaces, such as replace spaces .

$mytext = str_replace(' ', ' ', $mytext); 

Below is a demonstration of implementing   using JavaScript:

let str = "I'm trying to write my text in one line I'm trying to write my text in one line I'm trying to write my text in one line"; $('#d1').html(str); str = str.replace(/\s/g, ' '); $('#d2').html(str);

Simply using preg_replace is sufficient. In case you only want to substitute newlines, you can attempt the following method:

$mytext = 'I"m trying to write my text in one line'; echo preg_replace('/\n/', ' ', $mytext); // prints I"m trying to write my text in one line 

In a multi-line string, the line break will be recognized as a newline character even if you don’t explicitly include it. Hence, to identify it in your regex, you can use the code .

The cause for your current solution not functioning as desired is that it includes HTML line break elements in the string instead of newline characters, as mentioned in the documentation available at https://www.php.net/manual/en/function.nl2br.php. Upon executing your solution, the printed output shows that the newline characters are getting eliminated while the HTML elements added by nl2br remain unaffected.

Kindly make sure to add the faulty output you are encountering to your post in the future, along with the code.

How to add a new line/linebreak in php code, \n is a line break. use of \n with. 1. echo directly to page. Now if you are trying to echo string to the page: echo «kings \n garden»;.

New Line and Line Breaks in PHP

In this video, I will show you how to use new line characters to create more readable code. I
Duration: 4:34

Preserve line break in string inside array

My goal is to display the string that was received by the API.

but when I print it using this

The following is the way it is printed on a single line.

This RV features a front bedroom in the bunkhouse, a kitchen island, a loft, and an outdoor kitchen. It also has four slide-outs to provide ample space.

  • Bunkhouse
  • Front Bedroom
  • Kitchen Island
  • Loft
  • Outdoor Kitchen
  • 4 Slide-Outs

Can you provide assistance on obtaining the desired output?

Use nl2br for regular line breaks

To solve the problem, implement the php explode function and use a loop (any type of loop) to iterate over each value. It would be beneficial if you could provide the entire code. Assuming that the received data contains
tags and numerous whitespaces, I will proceed with the following assumptions: 1) The tag used was
. 2) Try the code below.

The code may become complicated if I presume the presence of excessive white spaces within the string.

PHP doesn’t show line break in textarea value in echo, The text contain break line \n that you should convert it to br in html. – Mohammad. Sep 23, 2018 at 8:38 · Attach html tags to newlines. nl2br —

PHP line break between content and date

My objective is to split the title and date into two separate lines as they are currently appearing on a single line.

PHP novice here needs help!

To avoid using the nl2br() function, simply include a line break.

To avoid mixing PHP and HTML, it would be best to separate the dynamic content from the HTML layout. You can achieve this by using the following approach while jumping in and out of PHP and HTML.

Once you have generated the output of your two functions, you can customize the appearance of the HTML elements to match your desired layout. The provided code will simply display the output of both functions on distinct lines.

have the brake not in php

Источник

text comin back in one line

would you know why would a text comes back in one line, i am trying to implement this pm system where i posted for some other isue, but this one is strange. when i am composing a message and keep writing, in the message body i have the similar window like i am typing here and when the line finishes it goes down and continues and if i enter
like this it goes to one down but after i send the message i go to inbox to view it but it comes in one line, does not limit the chracters typed in one line and does not see the enter, instead of going one line down when entered it just levaes a space. would you know how i could fix this? thank you

  • 5 Contributors
  • 24 Replies
  • 163 Views
  • 1 Week Discussion Span
  • Latest Post 14 Years Ago Latest Post by mschroeder
$message_with_breaks = nl2br($_POST['message']);

I don’t understand, where is the textarea that you mentioned in your earlier post ?

Change 75 to whatever width you want.

The wordwrap function breaks your content down with PHP_EOL which is a constant that is defined to match the proper end of line charachter for your operating system.

Then uses nl2br to convert them to
‘s including any …

All 24 Replies

$message_with_breaks = nl2br($_POST['message']);

Blah blah you arent logged in and stuff, you should do that or something


"; > else < //Query the database to see how many messages the logged in user has, then do a little math //Find the percentage that your inbox is full (message count divided by 50) //50 messages maximum, you can change that $sql = mysql_query ("SELECT pm_count FROM users WHERE username='$user'"); $row = mysql_fetch_array ($sql); $pm_count = $row['pm_count']; //This is the math to figure out the percentage. //The message could divided by 50 then multiplied by 100 so we dont have a number less than 1 $percent = $pm_count/'50'; $percent = $percent * '100'; ?>

Inbox | Compose | Sentbox

Please compose a message.


//Since this form was partially filled out we need to return an error message else < if (!$reciever) < $error = 'You must enter a reciever to your message'; >if (!$subject) < $error = 'You must enter a subject'; >if (!$message) < $error = 'You must enter a message'; >//If the variable error is not set to zero, we have a problem and should show the error message if($error != '0') < echo "

$error


"; > //There are no errors so far which means the form is completely filled out else < //Are the trying to send a message to a real user or to something they just made up? $user_check = mysql_query("SELECT username FROM users WHERE username='$reciever'"); $user_check = mysql_num_rows($user_check); //The user is real and not made up if this is true if($user_check >'0') < //There might already be a sessioned time variable, if so we need to get it for the flood check $time = $_SESSION['time']; //If there is a time variable already, set it to the varialbe $old_time if($time >'0') < $old_time = $time; >//Here we get the minutes and seconds on the server time using the date function, and set that to the $time variable //Now we find the difference between this time ($time) and the time that the page was submitted ($old_time) $time = date('is'); $difference = $time - $old_time; $_SESSION['time'] = $time; //If the two times have a difference greater or equal to 15, which is 15 seconds, they can submit the message, this is for flood protection if($difference >= '15') < //Get their private message count $sql = mysql_query ("SELECT pm_count FROM users WHERE username='$reciever'"); $row = mysql_fetch_array ($sql); $pm_count = $row['pm_count']; //You cant have more than 50 private messages, if they try sending a message to a user with a full inbox return an error message if(pm_count == '50') < $error = 'The user you are trying to send a message to has 50 private messages, sorry but we cant send your message untill that user deletes some of their messages.'; >else < //And now we stick the message in the database with all the correct information mysql_query("INSERT INTO messages (reciever, sender, subject, message) VALUES('$reciever', '$user', '$subject', '$message')") or die (mysql_error()); //Add 1 to the pm count, update the reciever with the new pm count $pm_count++; mysql_query("UPDATE users SET pm_count='$pm_count' WHERE username='$reciever'"); >//Let the user know everything went ok. echo "

You have successfully sent a private message!


"; > //Since they are trying to send messages faster than every 15 seconds, give them an error message else < $error = 'You must wait 15 seconds before sending another private message'; >> //If they mis spelled or, made up a username, then give an error message telling them its wrong. else < $error = 'That username does not exist, please try again. Remember to check your spelling, and don\'t make stuff up at random.'; >> > //Since we may have set the error variable to something while trying to send the messae, we need another error check if($error != '0') < echo "

$error


"; > else < //Here's the form for the input ?>

Username

">

Subject

">

Message Body

> ?>
//We need to grab the msg_id variable from the URL. $msg_id = $_REQUEST['msg_id']; //Get all of the information about the message with the id number sent through the URL $view_msg = mysql_query("SELECT * FROM messages WHERE "); $msg = mysql_fetch_array($view_msg); $reciever = $msg['reciever']; $sender = $msg['sender']; $subject = $msg['subject']; $message = $msg['message']; //If the person who is supposed to recieve the message is the currently logged in user everything is good if($reciever == $user) < //The message was recieved, so lets update the message in the database so it wont show up in the sent page any more mysql_query("UPDATE messages SET recieved='1' WHERE "); //Query the database to see how many messages the logged in user has, then do a little math //Find the percentage that your inbox is full (message count divided by 50) //50 messages maximum, you can change that $sql = mysql_query ("SELECT pm_count FROM users WHERE username='$user'"); $row = mysql_fetch_array ($sql); $pm_count = $row['pm_count']; //This is the math to figure out the percentage. //The message could divided by 50 then multiplied by 100 so we dont have a number less than 1 $percent = $pm_count/'50'; $percent = $percent * '100'; //Now we will display the little navigation thing, the fullness of the inbox, then display message information stuff, like who its from, the subject, and the body ?> 

Inbox | Compose | Sentbox

From:

">

Subject:

Message Body:

//Everything is not good, someone tried to look at somone else's private message else

Источник

Оцените статью