View Full Version : php convert text to html
05-14-2006, 03:45 PM
I have some poems in my database.. they show nice inside the database.. no problem with them.. formatted properly.. when I try to display them.. they show awful, they show as short stories, with no lines between the verses..
How do I solve this from my PHP code ? please help.. thanks!
05-14-2006, 04:47 PM
You should be able to enter the html into the db and have it displayed as such... normally the most efficient way. There are a few rich text editors out there that will make it easy for you.
05-14-2006, 10:21 PM
efficient thaught dilli, but I already have almost 400 records into my db, that I wouldn't like to edit manually.. I'd like to display them..
told you.. they show nice inside the db.. that's the thing that gives me hope that they can be settled..
05-15-2006, 03:45 AM
You can do that too, not being a php'r I had to look this up. Let us know how you do.
if ($_SERVER['REQUEST_METHOD'] == 'POST') //if you submited the form then
$message = $_POST["message"]; //message is the text field in the form
$message=eregi_replace(chr(13),"<br>",$message); // replace chr(13) ( Line
Break Char ) with <br> ( Line break html code )
echo $message; // print the variable
05-15-2006, 05:44 AM
[QUOTE=dilligaf]not being a php'r I had to look this up. [/QUOTE]Thought you were learning php? How's that going?
05-15-2006, 06:00 AM
About as well as you can see above. Got enough to make some simple modifications to existing applications but not near enough to pretend that I have a clue. (smiley here) Always seem to run back to the tried and true.
Got a couple of new MFA sites up, and a few more on the drawing board. They all use the same php resources so maybe it'll help with the comfort zone.
05-15-2006, 09:33 AM
[QUOTE=flaviussirop]I have some poems in my database.. they show nice inside the database.. no problem with them.. formatted properly.. when I try to display them.. they show awful, they show as short stories, with no lines between the verses..
How do I solve this from my PHP code ? please help.. thanks![/QUOTE]
php has a built in function for this, nl2br()
05-16-2006, 02:58 AM
thanks a lot guys.. I just tried dilli's code.. works just perfect!!! LOVE THIS FORUM!.. one more problem to go.. displaying query results on multiple pages with links from one to another ..
05-21-2006, 09:41 PM
[QUOTE=flaviussirop] one more problem to go.. displaying query results on multiple pages with links from one to another ..[/QUOTE]
not that hard actually :)
how many results you want on one page? - $s = user defined
how many results you have available? - $r = mysql_num_rows() usually
number of pages you can can generate - $t = round($r/$s + .4)
with that information you can use the mysql limit function to grab selected rows within a query AND generate links to pages.
06-24-2006, 02:50 PM
06-24-2006, 02:55 PM
I wrote some heavily commented pagination code a while back, that if you read the comments, you should be able to learn from it. I just cleaned up the formatting slightly, so hopefully there aren't any parse errors that I missed.<?php
/* Replace the following credentials with yours */
$server = "localhost";
$username = "user";
$password = "pass";
$database = "your_db";
$table = "your_table";
// Connect To MySQL Server
@mysql_connect($server, $username, $password) or die("Couldn't Connect to Database");
// Select Database
@mysql_select_db($database) or die("Couldn't Select Database");
// set number of results to display per page (in this case, 10 per page)
$pagelimit = "10";
// run query
$strSQL = mysql_query("SELECT COUNT(*) as totalrows FROM $table") or die(mysql_error());
$row = mysql_fetch_assoc($strSQL);
// count number of matches
$totalrows = row["totalrows"];
// determine how many pages there will be by using ceil() and dividing total rows by pagelimit
$pagenums = ceil ($totalrows/$pagelimit);
// if no value for page, page = 1
// create a start value
$start = ($page-1) * $pagelimit;
// blank matches found
echo "<b>" . $totalrows . " matches found</b><br>\n";
// Showing Results 1 to 10 (or if you're page limit were 15) 1 to 15, etc.
$starting_no = $start + 1;
if ($totalrows - $start < $pagelimit)
$end_count = $totalrows;
if ($totalrows - $start >= $pagelimit)
$end_count = $start + $pagelimit;
echo "Results $starting_no to $end_count shown.<br>\n";
// create dynamic next, previous, and page links
/* lets say you're set to show 10 results per page and your script comes out with 12 results.
this will allow your script to say next 2 if you're on the first page and previous 10 if you're on the second page. */
if ($totalrows - $end_count > $pagelimit)
$var2 = $pagelimit;
if ($totalrows - $end_count <= $pagelimit)
$var2 = $totalrows - $end_count;
$space = " ";
// previous link (if you're on any page besides the first, create previous link)
echo "« <a href='" . $PHP_SELF . "?page=".($page-1)."' class=main>Previous" . $space . $pagelimit . "</a>" . $space . $space . "";
// dynamic page number links
for ($i=1; $i<=$pagenums; $i++)
echo " <a href='" . $PHP_SELF . "?page=$i' class=main>$i</a>";
echo " <b>[".$i."]</b>";
// next link (if the page you are on is less than the total amount of page numbers, there are more pages left)
echo "" . $space . $space . $space . $space . " <a href='" . $PHP_SELF . "?page=".($page+1)."' class=main>Next " . $var2 . "</a> »";
/* output your data wherever you'd like.
in order for this all to work, before outputting your data, you have to run the query over using MySQL's LIMIT.
This will limit how many results are actually displayed on the page. */
$strSQL = mysql_query("SELECT * FROM $table LIMIT $start,$pagelimit") or die(mysql_error());
// LIMIT 0,10 will start at 0 and display 10 results
// LIMIT 10,5 will start at 10 and display 5 results
/* now you can do whatever you'd like with this query. it will only output ten results per page.
change the $pagelimit variable to whatever to output more than 10 result per page. */
vBulletin v3.0.3, Copyright ©2000-2013, Jelsoft Enterprises Ltd.