Jump to content

[SOLVED] Displaying paragraphs in textbox


fri3ndly

Recommended Posts

Hiya

 

I have a database table with a row containing text in paragraphs.

 

I have a textfield in an admin control panel that will bring up that text for editing, but I do not want it to display <p></p> where paragraphs are, I want it to display as it would on a webpage in paragraphs instead to make it easier for edititng.

 

Can someone help me achieve this? I have tried strip tags, but this just makes all of the text one paragraph.

 

Thanks

Link to comment
Share on other sites

Try something like:

 

<?php
str_replace( '<p>', '', $v );
str_replace( "</p>", "\n", $v );
?>

...where $v is the content you want to show in the textarea. I'm not sure if "\n" will work, if not just change the second parameter of the second str_replace to ""... but I added the "\n" there so that... well if you didn't then I think it'd be all crumpled up.

Link to comment
Share on other sites

Thanks, I can see how that could work, but it seems to be displaying the paragraphs tags.

 

Is there a way to reverse a function like this:

 

function formatString($string)
{
     $x_string = explode("\r\n\r\n",trim($string)); // explode on double carriage returns
     $string = str_replace('<p></p>','','<p>' . implode('</p><p>',$x_string) . '</p>');
     return $string;

}

 

This function will make sure that if paragraphs are entered in a textbox, the <p> tags are included, so I thought there must be a way to reverse it?

 

Link to comment
Share on other sites

Just incase this helps anybody else, I found a solution to the problem, although its probably not the best thing to do, it works!

<?php
function nl2p($your_text) {
 return "" . str_replace("</p><p>", "\n\n", $your_text) . "";
}
?>

 

Then, where you need to format text for a textbox display call the function like this:

<?php echo strip_tags (nl2p($your_text)); ?>

 

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.