Jump to content

[SOLVED] I want to insert paragraph tags into my code instead of carriage returns.


Peggy

Recommended Posts

I know this should be simple but I can't figure it out?

First of all, I have a form that populates a database. I want to insert '<p></p>' tags every time the user hits enter.

When I look in the data base for '\r\n' or  '\n'  or '\r'  I don't see them. When I export the data from the data base, then I see them. This is okay I can figure that out but why does this code not work?

 

$search = array('\r\n','\n','\r');

str_replace($search, '<p></p>',$category_describe_long);

 

This has to be something that is done all the time.

 

Link to comment
Share on other sites

The reason why it's not working the way you might expect is because "\n" != '\n'. '\n' will be the literal \n and "\n" will be a line break.

 

$search = array("\r\n","\n","\r");

 

I doubt that'll work the way you intended though, it doesn't make much sense.. If you had:

 

Text Text Text
Text Text Text

 

That would change it into:

 

Text Text Text<p></p>Text Text Text

 

edit: Whoops, forgot to post the solution to what I think you might be after.. I think you might be after something like this:

 

$text = "Text Text Text\nText Text Text";
$text = preg_replace('~^(.+)$~m', "<p>$1</p>", $text);
echo $text;

 

Output:

<p>Text Text Text</p> 
<p>Text Text Text</p>

Link to comment
Share on other sites

\n \r are hidden tags so to speak.

 

Warning this is a bad explanation: They are printer tags. In the case of web sites. When someone goes to print the page. The printer software software recognizes the <br> <p>  tags as \n \r and makes the spacing for the print page accordingly.

 

So seeing as your having your users populate the database with the form and the data is being pulled from the database and then displayed. Why not just either append paragraph tages either to whats being store or where the data is being displayed.

 

If while in storage add the tags <p> user_input </p>

Or when displayed <p> database_output </p>

 

But if your looking to do something all together diffrent try looking up nl2br() and reading about that function a bit. That also ties into the tags your talking about.

Link to comment
Share on other sites

Thank You!!!

I did not realize that there was a difference between using double or single quotes. Except of course when the string is being inserted between existing quotes.

Anyway, Thank-You again!

Oh ya, the reason that I am inserting the paragraph tags that way is: when the data goes back into the web page the beginning <p> tag and ending </p> are already there.  :)

Link to comment
Share on other sites

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.