Jump to content

String Manipulation- how to preserve new line ... complicated help plz!


Recommended Posts

Hey guys, so I wrote a quick little script to help me with some text manipulation that I have to do. Here is the basic gist of it:

 

I get a header, body, and footer text.

I copy- paste these strings from a TEXT FILE to a FORM, which submits these values as POST.

I hit "submit", and my script "joins them together", and replaces some names (and more string manipulation).

It then outputs the result on the next page.

So everything works, my string is manipulated like I want it to be, but...

 

My problem is that when I copy- paste a 3 paragraph for the body, when my script outputs it, it is all compressed into 1 large paragraph. Would anyone know how I could "replace" the new line character with a "<br>" somehow, so that in the outputted html my body retains the same structure?

 

I hope this makes sense to you guys, if not please ask for clarification. Thank you!!

nl2br doesn't seem to be working..

Any Idea why? Here is my code:

 

if(isset($_POST['original']))
$original =  nl2br($_POST['original']);
echo $original;

<form name="input" action="maketext.php" method="post">
<table border="1" cellspacing="1" cellpadding="1">
  <tr>
    <td>Original:</td>
    <td><input type="text" name="original" /></td>
  </tr>
  <tr>
//....

 

when I copy- paste a two paragraph text, it comes out fused to 1 paragraph! :confused::shrug:

where do you copy it from? Word or something? I'm guessing that the text itself doesn't have new line characters, and thats why the nl2br() function won't work. You can insert <br /> tags or better yet, <p> tags into the textarea manually, but I don't see a way  for php to guess where your paragraphs ends/begins and insert a tag there

Well, I am actually using the script to manipulate wordpress posts, so that is where I am copying the text from. However, I tested the script on sample text from a text file in (text edit in os X), and I had the same problems.

 

EDIT:

-I also tested it on random paragraphs copied from a web page, I get the same problem again!! It never works  :'( :confused:

I think I realized my problem, but I do not know how to fix it. Here is the deal- When I try to type a value manually, without copy and paste, I cannot add a second paragraph by pressing "enter/return". When I finish writing my first paragraph and hit "enter/return" to start my second, it automatically submits only my first paragraph. This leads me to believe that my form is not set up properly, to even "take in" "enter/return" new lines. Does anyone know how I could fix this? Here is what I have now:

 

<form name="input" action="maketext.php" method="post">
<table border="1" cellspacing="1" cellpadding="1">
  <tr>
    <td>Original:</td>
    <td><input type="text" name="original" /></td>
  </tr>
  <tr>
//....

 

perhaps the input type should be textarea? idk much about this stuff, I really appreciate everyone's help!

 

what do you think??

Ok, could somebody clear up how exactly I would make the input be a textarea? I have never used it before...

 

Here is what I am trying and it is not working:

 

<form name="input" action="maketext.php" method="post">
<table border="1" cellspacing="1" cellpadding="1">
  <tr>
    <td>Original:</td>
    <td><input type="textarea" name="original" /></td>
  </tr>
  <tr>
    <td>Author Name:</td>
    <td><input type="text" name="author" /></td>
  </tr>
  <tr>
//...

 

How do I do that, so that it's "name" is "original", so that it will be submited with the form, and can be accessed at the next page from $_POST['original'].

 

Thank you!!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.