astralaaron Posted April 7, 2008 Share Posted April 7, 2008 Hello, I am comfortable pulling arrays of data from a database. for message forms / news feeders, those types of things.I am sure there is a way, but when I output my data it doesn't format how i typed it to enter it into the database - formatted in paragraphsand what not.. I am guessing people here already know what I am talking about so I will stop the explanation right there.A Link to comment Share on other sites More sharing options...
astralaaron Posted April 7, 2008 Author Share Posted April 7, 2008 this message forum is a good example.. how it let me push enter to move my 'A' down a few spaces... Link to comment Share on other sites More sharing options...
justsomeguy Posted April 7, 2008 Share Posted April 7, 2008 Use nl2br to convert newline characters to <br> tags. Link to comment Share on other sites More sharing options...
astralaaron Posted April 7, 2008 Author Share Posted April 7, 2008 Use nl2br to convert newline characters to <br> tags.let me get this straight. when someone pushes enter it creates an invisible \n ? Link to comment Share on other sites More sharing options...
astralaaron Posted April 7, 2008 Author Share Posted April 7, 2008 Ok that works for new lines...one more problem, "Hello world" outputs "Hello World"I was at first using trim() and I took it off thinking that was what "trims" the spaces... but it is still the same..?any suggestions? Link to comment Share on other sites More sharing options...
astralaaron Posted April 7, 2008 Author Share Posted April 7, 2008 ahh looks like this forum doesnt allow the spaces... basically i need "Hello space space space space space World"not "Hello World" Link to comment Share on other sites More sharing options...
kv79 Posted April 7, 2008 Share Posted April 7, 2008 As I remember I cant type a code for spacing because this forum is ...so this is the link http://www.w3schools.com/tags/ref_entities.aspwc3school call it "non-breaking space"I hope this helps. Link to comment Share on other sites More sharing options...
jeffman Posted April 7, 2008 Share Posted April 7, 2008 From the beginning, HTML has interpreted any combination of line breaks and empty spaces as exactly one space. On the other hand, formatting with non-breaking spaces is very inexact. You'd do better with CSS. Link to comment Share on other sites More sharing options...
astralaaron Posted April 7, 2008 Author Share Posted April 7, 2008 As I remember I cant type a code for spacing because this forum is ...so this is the link http://www.w3schools.com/tags/ref_entities.aspwc3school call it "non-breaking space"I hope this helps.i know about the but how do you do it for each space someone presses? Link to comment Share on other sites More sharing options...
astralaaron Posted April 7, 2008 Author Share Posted April 7, 2008 From the beginning, HTML has interpreted any combination of line breaks and empty spaces as exactly one space. On the other hand, formatting with non-breaking spaces is very inexact. You'd do better with CSS.eh this just confused me even more, thanks for trying though Link to comment Share on other sites More sharing options...
kv79 Posted April 7, 2008 Share Posted April 7, 2008 -> astralaaronbut how do you do it for each space someone presses?I do not understand exactly what are you asking ? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 7, 2008 Share Posted April 7, 2008 You can use str_replace if you want to replace spaces with " ". The text might come out a little strange though if it only wraps on newlines, it will not wrap on the nbsp character. let me get this straight. when someone pushes enter it creates an invisible \n ?It's not invisible, when you press enter it adds a newline character. The same as tab or whatever else. Link to comment Share on other sites More sharing options...
astralaaron Posted April 7, 2008 Author Share Posted April 7, 2008 You can use str_replace if you want to replace spaces with " ". The text might come out a little strange though if it only wraps on newlines, it will not wrap on the nbsp character.It's not invisible, when you press enter it adds a newline character. The same as tab or whatever else.well I don't see it when I press enter so I just said invisible... of course it is there .. if enter leaves a \n what does a tab leave?.so basically what everyone is saying is there is no way to format text perfectly through php? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 7, 2008 Share Posted April 7, 2008 Enter doesn't leave a "\n", it adds a newline character. In PHP you represent a newline character using the escape sequence "\n". In PHP a tab character is represented by "\t". so basically what everyone is saying is there is no way to format text perfectly through php?That depends on the definition of "perfectly". Link to comment Share on other sites More sharing options...
astralaaron Posted April 7, 2008 Author Share Posted April 7, 2008 Enter doesn't leave a "\n", it adds a newline character. In PHP you represent a newline character using the escape sequence "\n". In PHP a tab character is represented by "\t".That depends on the definition of "perfectly".well by perfectly, all I meant was the same way the user types it into the textfield is how it should output.. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 7, 2008 Share Posted April 7, 2008 That is how PHP outputs it. PHP outputs exactly what you type in. The problem is that HTML does not display it that way, HTML ignores whitespace. You can use the CSS white-space property to change how the browser handles whitespace:http://www.w3schools.com/css/pr_text_white-space.asp Link to comment Share on other sites More sharing options...
kv79 Posted April 7, 2008 Share Posted April 7, 2008 ->astralaaron It's not invisible, when you press enter it adds a newline character. The same as tab or whatever else.This is ,how I understand this .There is a few coding world when you are watching your computer ,but you are not aware of that .The World Wide Web Consortium (W3C)Tim starts work on a hypertext GUI browser+editor using the NeXTStep development environment. He makes up "WorldWideWeb" as a name for the program .http://www.w3.org/History.html-> And the www. system have a rules for writing and it not prefect like any other (create by W3C)The world of www but NeXTStep is developed by some other program wich I can't find on the web , but I will ask later a 65 years old developer man . -> the world of second language also have a bugs Assembler -> first level language world of machine codehardware , what to say ?, ->the our world .Hope this was a helpful Tell me if anything of this wrong informed .I would be pleased . Link to comment Share on other sites More sharing options...
kv79 Posted April 7, 2008 Share Posted April 7, 2008 Maybe I miss the whole theme . Link to comment Share on other sites More sharing options...
astralaaron Posted April 7, 2008 Author Share Posted April 7, 2008 That is how PHP outputs it. PHP outputs exactly what you type in. The problem is that HTML does not display it that way, HTML ignores whitespace. You can use the CSS white-space property to change how the browser handles whitespace:http://www.w3schools.com/css/pr_text_white-space.asp ok now the other guys post makes sense. Link to comment Share on other sites More sharing options...
kv79 Posted April 7, 2008 Share Posted April 7, 2008 What justsomeguy typed is total true , but in which world . But in basic you need to find a answer by your self .This forum is like a little light of whole dark world . So you can see where are you going at least . Link to comment Share on other sites More sharing options...
astralaaron Posted April 7, 2008 Author Share Posted April 7, 2008 ok well....normal didnt change anything.pre made it so the white spaces worked like I wanted BUT it did not wrap at all.. just kept going off to the right of the page, no 'newlines'and of course nowrap makes it not wrap.. and not pay attention to spaces.is there a way to make both the newline / spaces work at the same time? Link to comment Share on other sites More sharing options...
astralaaron Posted April 7, 2008 Author Share Posted April 7, 2008 ok well....CSS Whitespace propertynormal didnt change anything.pre made it so the white spaces worked like I wanted BUT it did not wrap at all.. just kept going off to the right of the page, no 'newlines'and of course nowrap makes it not wrap.. and not pay attention to spaces.is there a way to make both the newline / spaces work at the same time? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 7, 2008 Share Posted April 7, 2008 You should still be using nl2br to convert newlines to <br> tags. Link to comment Share on other sites More sharing options...
jeffman Posted April 7, 2008 Share Posted April 7, 2008 What this calls for is a regular expression. I'm looking into it, but it's going to be a complicated one, for me at least. Link to comment Share on other sites More sharing options...
jeffman Posted April 7, 2008 Share Posted April 7, 2008 Okay. This works. Apparently, when preg_replace() works with arrays, it does so sequentially, which I would not have guessed. This makes possible the really sloppy regex work that I am presenting. I'm sure a talented regex genius could have done all this in a single expression. Not I.The goal is this: change every \n character into <br>. Preserve the author's desire to have multiple spaces. So change " " to " " BUT don't change single spaces to " ", and make sure that every group of multiple " " begins with a real space. This way text wrapping won't be affected.Cheers! $string = //(whatever comes out of your textarea)$pat[0] = '/\n/';$rep[0] = '<br>';$pat[1] = '/\s/';$rep[1] = "\t";$pat[2] = '/(\S)\t/';$rep[2] = '$1 ';$pat[3] = '/\t/';$rep[3] = ' ';ksort($pat); // a precaution in case you adapt the routine so the arrays are filled unsequentiallyksort($rep);$string = preg_replace($pat, $rep, $string);echo $string; // or do something useful with it. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.