9 Replies - 3485 Views - Last Post: 22 April 2011 - 09:09 AM Rate Topic: -----

#1 davey123  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 103
  • Joined: 24-December 10

Inserting line breaks in variable text with echo command?

Posted 22 April 2011 - 06:49 AM

Im a total noob with php and learning through a book I bought.

Im doing a simple form mailer at the moment and using the echo command for my thank you page.
The problem Im getting is if the comment section is a few lines in length in the form and when echo calls after the submit button is clicked, the text is all 1 line and runs off my page.

How do I insert line breaks in php when it takes variables from the form?
Is This A Good Question/Topic? 0
  • +

Replies To: Inserting line breaks in variable text with echo command?

#2 Travis1012  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 220
  • Joined: 05-August 09

Re: Inserting line breaks in variable text with echo command?

Posted 22 April 2011 - 07:01 AM

There are a few ways to put words over several lines
  • use the html command <br /> to create a new line. Just add that string in where you need it and it should go to the next line. This is good when you know when to put in a new line.
  • use the CSS command white-space.


It all depends on what you need to do.

We all have to start somewhere so dont worry about it :)
Was This Post Helpful? 0
  • +
  • -

#3 davey123  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 103
  • Joined: 24-December 10

Re: Inserting line breaks in variable text with echo command?

Posted 22 April 2011 - 07:18 AM

That doesn't work or I don't understand.
Let me explain a little better.

If my comment section is a few lines deep in my text area on my form page and when I echo it it all comes out 1 line across in my thank you page.

How do I insert <br/> in my echo command or even in my comment form since the text input is automatically contained within the comment box?

I tried using whitespace on the div containing the div with the echo command but it didnt work.
I also tried putting the echo command inside a table and set the table width but it enlarged the table also.

I cant figure out how to set the width of a text variable when it's echoed.

Im confused.
Was This Post Helpful? 0
  • +
  • -

#4 Travis1012  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 220
  • Joined: 05-August 09

Re: Inserting line breaks in variable text with echo command?

Posted 22 April 2011 - 07:36 AM

Just been thinking about it if the comments are over several lines then there is a \n at the end of each line when it is submitted. This does not display anything on the screen but is detected by the PHP. If you replaced all of the \n in the comment with <br /> tags it should format it to be like the text entered by the user.

EDIT: I think I am right

This post has been edited by Travis1012: 22 April 2011 - 07:36 AM

Was This Post Helpful? 0
  • +
  • -

#5 davey123  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 103
  • Joined: 24-December 10

Re: Inserting line breaks in variable text with echo command?

Posted 22 April 2011 - 07:43 AM

But how?

The comments are dynamic text.
When its echoed, php just grabs what's typed in the variable and displays it.
It could be 3 words or 300, so the /n will have to be generated automatically when the sentence gets 200 pxl long.
Was This Post Helpful? 0
  • +
  • -

#6 Travis1012  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 220
  • Joined: 05-August 09

Re: Inserting line breaks in variable text with echo command?

Posted 22 April 2011 - 07:49 AM

The preg_replace PHP command should do it.
Was This Post Helpful? 0
  • +
  • -

#7 davey123  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 103
  • Joined: 24-December 10

Re: Inserting line breaks in variable text with echo command?

Posted 22 April 2011 - 07:54 AM

I don't think that will be useful.

The preg_replace function is used to do a find-and-replace on a string or an array. We can give it one thing to find and replace (for example it seeks out the word 'him' and changes it to 'her') or we can give it a full list of things (an array) to search for, each with a corresponding replacement.

I want to make dynamic text sentences at a fixed width. Not replace anything.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6053
  • View blog
  • Posts: 23,488
  • Joined: 23-August 08

Re: Inserting line breaks in variable text with echo command?

Posted 22 April 2011 - 08:16 AM

chunk_split?
Was This Post Helpful? 1
  • +
  • -

#9 davey123  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 103
  • Joined: 24-December 10

Re: Inserting line breaks in variable text with echo command?

Posted 22 April 2011 - 09:03 AM

Thanks for the direction and something caught my eye on that page... http://www.php.net/m...on.wordwrap.php

Wordwrap. Thats what Im looking for but I can't get it to work. Its still producing 1 line of text.

<div id="thankdiv"><?php
	  $comments = $_POST['comments'];
	  $newcomments = wordwrap($comments, 30, "<br/> \n");
	  echo ' Your comments : ' .$newcomments;
	   ?>
</div>


Was This Post Helpful? 0
  • +
  • -

#10 davey123  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 103
  • Joined: 24-December 10

Re: Inserting line breaks in variable text with echo command?

Posted 22 April 2011 - 09:09 AM

Got it!

<div id="thankdiv"><?php
	  $comments = $_POST['comments'];
	  $newcomments = wordwrap($comments, 30, "\n", true);
	  echo ' Your comments : ' ."$newcomments\n";
	   ?>
</div>



Thanks for lead Jack!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1