Jump to content

Text Boxes & <br> PHP

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Bioshox

Bioshox

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
I know the person who's probly going to help me with this, is getting sick of it :P!!!

But anyway

I have a Text box in PHP on my website for entering a post into a blog, but I want it to automaticly add <br>'s on new lines when Enter is pressed!

Any code for this, as now I have added stripslashes and htmlspechcahars, it just shows <br>!!!!!

#2
Guest_Jaan_*

Guest_Jaan_*
  • Guests
Well.. you can use

$str = "I think I have <br />found a solution<br />to my problem!";
$str = str_replace("<br />", "\n", $str);
echo $str;

You needed something like this ?

#3
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Haha bioshock :P

If you save the text of a textbox inside the database and retrieve it again on another page of your website to show it ( for example news or posts that you want to add to a database by typing it in a textbox and then show on your website ), if this is what you're trying to do, the function nl2br can be used which will convert normal enter inputs automaticly into br's (<br>).

So say you've got a post retrieved from the database and put in a variable $post, then to show it correctly ( enters replaced by html <br> tags to show these enters properly ):

echo nl2br($post);

EDIT: misread your post, I think this is the reverse effect of what you mean, Jaan's solution should work I suppose :)

However I'm not quite sure how come it shows <br> inside your posts? As I suppose you just insert a message like this to the database:

Quote

Line 1 of my message

Line 2 of my message

As there's no need to put <br> html tags inside your post, you can just use normal enters and use the function nl2br as mentioned above into automaticly convert them to proper html. So if you did add <br> tags to your post manually to add enters for your posts, that's not neccessarely needed. However Im not sure how you wanted your system to work, so ^^

#4
Bioshox

Bioshox

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
This works but now it dosent include line breaks and everything is just one paragraph!

Jaans code didn't work at first so I modified it to this

	$row['body'] = str_replace("<br>", " ", $row['body']);  

As you can see it's replacing <br>'s with spaces, I tried putting /n in put it just displayed '/n'

#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
it's supposed to be \n not /n
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#6
Bioshox

Bioshox

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts

Orjan said:

it's supposed to be \n not /n


It still didn't work D':

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
Are you using <input type="text"> or are you using <textarea> ? There is a big difference between them!
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#8
Bioshox

Bioshox

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
<textarea> for the Body

and <input type="text"> for the titles :)