View Full Version : How to add semantics to output strings

01-20-2008, 09:09 PM
Firstly, I would like to iterate that I'm very much new to php so I don't know many of the functions etc. I have built a little CMS for my site that allows members to post blogs and add them to the database. Then they are listed and the clients can view them if they click the right link... general stuff.

I'm curious about how the actual 'code' behind the blogs is output. I'm currently using nl2br to keep the line spaces in, but because everything else in the site is supposed to be semantic and 'properly' coded, I would like to apply this to the articles to. So, for example, rather than having two line breaks after each paragraph I would like the paragraphs to be wrapped in <p> tags so they look as my paragraphs should and I'm not having to use any horrible <br /> tags.

Is this possible, and if so how? And would it be done when the blog is added to the database, or upon return.

Also, on a side note, could someone quickly explain to me what exactly mysql_real_escape_string actually does, or what text it affects? Could I use it for this type of database input?

Thanks guys

01-21-2008, 12:46 AM
well if it really bothers you that much PHP has loads of string manipulation functions, you could for example ...

$input='<p>'.str_replace('<br /><br />','</p><p>',$input).'</p>';

Ideally you would leave the data in the database intact (in case you change your mind about formatting later), though re-parsing the same content every time you show it has performance implications, so thats a trade-off.

mysql_escape_real_string actually uses the mysql database to do the escaping, internally it calls a mysql function, unlike mysql_escape_string which simply fakes it at a php level.

01-21-2008, 01:14 PM
That was what I was originally going to look into doing and that would work really well for simple content... the only trouble is that sometimes headings are put into these blogs, which makes things a little more interesting :s