Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,401 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,215 people online right now. Registration is fast and FREE... Join Now!




txt to html formatting

 
Reply to this topicStart new topic

txt to html formatting

SoLi
post 13 Dec, 2002 - 05:40 PM
Post #1


andrewDust.com

*****
Joined: 27 Jan, 2002
Posts: 1,234


My Contributions


okay on this ocean website (http://www.dusty.pwp.blueyonder.co.uk/ocean/index.html) users can submit their own stories. they do this by entering their story into a text area. once, submitted the data is written to a text file. later on this story will need to be called up when other people want to view it, and it will be displayed on a html page.

now. is it possible, using php, for when the story is submitted, php scans the data and replaces any line gaps (indicating paragraphs) with the <p> tag? if so how. smile.gif

so for example. a user submits the following in the text area

"this is my story

this is the end of my story"

and then php needs to turn it into:

"this is my story<p>this is the end of my story"

after that it gets written to a text file, but thats one thing i do know how to do lol smile.gif

thx
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 14 Dec, 2002 - 12:13 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,172



Thanked 33 times

Dream Kudos: 25
My Contributions


I'm not sure what the PHP function is for it, but it would something similar to Replace(string,substring).
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 14 Dec, 2002 - 12:18 AM
Post #3


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,172



Thanked 33 times

Dream Kudos: 25
My Contributions


QUOTE(Amadeus @ Dec 14 2002, 04:13 AM)
I'm not sure what the PHP function is for it, but it would something similar to Replace(string,substring).

CODE

str_replace, apparently.

check out the usage here.
http://www.phpbuilder.com/mail/php-general...001102/1386.php
If that does not work, there's a thousand ways to go at the problem, from html.encode to user functions. Let me know the specific problem, and I will try to help.
User is offlineProfile CardPM

Go to the top of the page

klewlis
post 14 Dec, 2002 - 08:18 AM
Post #4


cur tu me vexas?

*****
Joined: 9 Nov, 2001
Posts: 1,723

please oh please oh please put </p>'s on there too!
User is offlineProfile CardPM

Go to the top of the page

MathewS
post 16 Dec, 2002 - 08:15 AM
Post #5


D.I.C Regular

***
Joined: 14 May, 2002
Posts: 252



Thanked 1 times

Dream Kudos: 1
My Contributions


i think you want something like
CODE
$text = str_replace ("\n", "<p>", $text);
User is offlineProfile CardPM

Go to the top of the page

SoLi
post 16 Dec, 2002 - 08:50 AM
Post #6


andrewDust.com

*****
Joined: 27 Jan, 2002
Posts: 1,234


My Contributions


thx that sort of worked but it entered a <p> every time a new line was entered, rather than every time there is an empty line.

so what u told me did this:

Line of text one
<P>Line of text two
<P> <<Gap indicating end of paragraph.
<P>Line of text three
<P>Line of text four

where as what i want is

Line of text one
Line of text two
<p> <gap indicating end of paragraph.
Line of text three
Line of text four.

although technically what has happened (with p's on every line) wont actually make a difference (because two consecutive Ps dont work, only the first will) it'd be nice just to see if its actually possible to get it how i want in the second example. can i not make php search my string for lines where no characters are present, and only enter the <p>'s on those lines?

thx alot everyone for the info so far. really appreciate it smile.gif
User is offlineProfile CardPM

Go to the top of the page

MathewS
post 16 Dec, 2002 - 12:25 PM
Post #7


D.I.C Regular

***
Joined: 14 May, 2002
Posts: 252



Thanked 1 times

Dream Kudos: 1
My Contributions


did you try
CODE

$text = str_replace ("\n\n", "<p>", $text);


which should check if they have two lines?
User is offlineProfile CardPM

Go to the top of the page

SoLi
post 16 Dec, 2002 - 02:18 PM
Post #8


andrewDust.com

*****
Joined: 27 Jan, 2002
Posts: 1,234


My Contributions


good idea but doesnt work. when using \n\n none of the <p> tags get inserted :S
User is offlineProfile CardPM

Go to the top of the page

gneato
post 16 Dec, 2002 - 03:49 PM
Post #9


<title>Untitled Document</title>

*****
Joined: 3 Sep, 2001
Posts: 1,311

that might be caused by stupid windows line-break and carriage-returns. In other words just remove all the \r's like this

$text = str_replace ("\r", "", $text);

and then

$text = str_replace ("\n\n", "<p>", $text);


Tell me if that works.
User is offlineProfile CardPM

Go to the top of the page

gneato
post 16 Dec, 2002 - 03:51 PM
Post #10


<title>Untitled Document</title>

*****
Joined: 3 Sep, 2001
Posts: 1,311

You might also want to do this after you replace all the \n\n's:

$text = str_replace ("\n", "<br>", $text);
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 07:53AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month