Peggy Posted November 20, 2009 Share Posted November 20, 2009 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. Quote Link to comment Share on other sites More sharing options...
Alex Posted November 20, 2009 Share Posted November 20, 2009 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> Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted November 20, 2009 Share Posted November 20, 2009 \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. Quote Link to comment Share on other sites More sharing options...
Peggy Posted November 20, 2009 Author Share Posted November 20, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.