Web Hosting Talk







View Full Version : php formatting help


markerpower
10-06-2005, 10:00 AM
I am trying to get the data that I input in a database to display exactly as how I inputted. I'm having problems where a \ comes before a ' and when I input paragraphs, it displays as one whole paragraph. I've came up with this code:


$description= preg_replace('/[^\w\s^.^,]/', '',$row['description']);
$description= preg_replace("/\\r*\\n/","<br />",$row['description']);
$description= preg_replace('\"', '"', $row['description']);
$description=stripslashes($row['description']);
But it seems that it doesn't display my data in paragraphs. Any suggestions?

Unknown_User
10-06-2005, 11:20 AM
Have you got magic-quotes-sybase (http://uk.php.net/manual/en/ref.sybase.php#ini.magic-quotes-sybase) enabled?

In order to remove the escaping of a single quote (double quotes, '\', etc) you need to parse the string through stripslashes();

In regards to the paragraphs what you will need to do before you place the data into the database is run the string through nl2br() which will change all new lines to <br />.

Christopher Lee
10-06-2005, 11:30 AM
Some quick thoughts:

I haven't looked over your code sequnce as a whole, nor have I tested it, but a few thoughts spring to mind:

1. You keep reassigning over top of the variable '$description'. A quick rewrite might be:

//assuming you are in a loop here.
unset($description); //empty it out.
$description = $row['description']; //fill it back up.

$description = preg_replace('/[^ws^.^,]/', '', $description);
$description = preg_replace("/\r*\n/","<br />", $description);
$description = preg_replace('\"', '"', $description);
$description = stripslashes($description);


2. The second line looks like you are trying to replace all newlines with break tags. Try the php function nl2br (http://us3.php.net/manual/en/function.nl2br.php). It might just be the ticket for what you are looking for.

3. I *think* line three's work is being replicated by the stripslashes use on line four.

That's all I can think of for now. If I have time tonight, I might try to refactor to offer you some alternatives, rather than this random fluff. If you come up with some alternatives before that, please post for the class. Good Luck! :)

markerpower
10-06-2005, 05:44 PM
Originally posted by DislexiK
Have you got magic-quotes-sybase (http://uk.php.net/manual/en/ref.sybase.php#ini.magic-quotes-sybase) enabled?

No it's not enabled. Is it suppose to be?

I now changed my code to:
$description= preg_replace('/[^\w\s^.^,]/', '',$row['description']);
$description= nl2br($row['description']);
$description=stripslashes($row['description']);

But it's still not breaking up my paragraphs.

tree-host
10-06-2005, 07:37 PM
Originally posted by markerpower
No it's not enabled. Is it suppose to be?

I now changed my code to:
$description= preg_replace('/[^\w\s^.^,]/', '',$row['description']);
$description= nl2br($row['description']);
$description=stripslashes($row['description']);

But it's still not breaking up my paragraphs.

Try this

$description= preg_replace('/[^\w\s^.^,]/', '',$row['description']);
$description= nl2br(nl2br($row['description']));
$description=stripslashes($row['description']);

Probably very bad pratice, but it might do it.

From what i remember nl2br doesnt actually replace \n with <br> but actually adds a <br> when it finds a \n meaning you can run it twice, and intheory get double <br>'s which might solve the problem.

markerpower
10-06-2005, 07:40 PM
I still get the same results.

tree-host
10-06-2005, 07:53 PM
dont suppose you can show me, or pm me a page where i can see the output can you?

markerpower
10-06-2005, 10:16 PM
The link is in my sig. The output I'm testing for this thread is under Newest Story that talks about a chimp.

Christopher Lee
10-06-2005, 10:21 PM
markerpower:

From your code it looks like you are still overwriting the $description variable each time you apply your filters...

markerpower
10-06-2005, 10:56 PM
Well then how do I not overwrite it?

tree-host
10-06-2005, 11:01 PM
Try this


$description= preg_replace('/[^ws^.^,]/', '',$row['description']);
$description= nl2br(nl2br($description));
$description=stripslashes($description);


If that doesnt work, try this...


$row['description']= preg_replace('/[^ws^.^,]/', '',$row['description']);
$row['description']= nl2br(nl2br($row['description']));
$row['description']=stripslashes($row['description']);

markerpower
10-06-2005, 11:27 PM
With the first one I get the same results. With the second one I get the same results, plus the slashes are added back into the text.

tree-host
10-06-2005, 11:33 PM
would you mind sending me the whole page? (oviosuly with passwords removed) Im starting to get the feeling there may be something else been strange.

markerpower
10-07-2005, 12:10 AM
<?php
class Pager
{
function getPagerData($numHits, $limit, $page)
{
$numHits = (int) $numHits;
$limit = max((int) $limit, 1);
$page = (int) $page;
$numPages = ceil($numHits / $limit);

$page = max($page, 1);
$page = min($page, $numPages);

$offset = ($page - 1) * $limit;

$ret = new stdClass;

$ret->offset = $offset;
$ret->limit = $limit;
$ret->numPages = $numPages;
$ret->page = $page;

return $ret;
}
}
?>
<?php


$page = $_GET['page'];
$limit = 20;
$result = mysql_query("select count(*) from submissions");
$total = mysql_result($result, 0, 0);

// work out the pager values
$pager = Pager::getPagerData($total, $limit, $page);
$offset = $pager->offset;
$limit = $pager->limit;
$page = $pager->page;

// create query
$query = "SELECT title, description, file, category, link, name, email, time, vidimg, id FROM submissions WHERE cat_id =3 order by id DESC limit $offset, $limit ";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

if ($page == 1) // this is the first page - there is no previous page
echo "Previous";
else // not the first page, link to the previous page
echo "<a href=\"stories.php?page=" . ($page - 1) . "\">Previous</a>";

for ($i = 1; $i <= $pager->numPages; $i++) {
echo " | ";
if ($i == $pager->page)
echo "Page $i |";
else
echo "<a href=\"stories.php?page=$i\">Page $i</a>";
}

//display data
while ($row = mysql_fetch_assoc($result)) {

$cat_id = $row['cat_id'];
$title = preg_replace('/[^\w\s^.^,]/', '',$row['title']);
$title=stripslashes($row['title']);
$file = $row['file'];
$time = $row['time'];
$description=$row['description'];
$row['description']= preg_replace('/[^ws^.^,]/', '',$row['description']);
$row['description']= nl2br(nl2br($row['description']));
$row['description']=stripslashes($row['description']);
$description = preg_replace( "`((http)+(s)?:(//)|(www\.))((\w|\.|\-|_)+)(/)?(\S+)?`i", "<a href=\"http\\3://\\5\\6\\8\\9\" title=\"\\0\">\\5\\6</a>", $description);
$name = $row['name'];
$email = $row['email'];
$link = $row['link'];
$id = $row['id'];

if ( $row['link'] == '' )
{
print "<p><font color=white><table width=\"100%\" bgcolor=#006699 border=1 border=0 cellspacing=0 cellpadding=0 bordercolor=#000000>
<tr>
<td colspan=3><a href=\"$file\">$title</a></td>
</tr>
<tr>
<td colspan=3>Link: <input name=title type=text id=title size=50 value=http://www.laughsgalore.com/display.php?id=$id> <p><div align=left>$description</div></td>
</tr>
<tr>
<td>Category: Stories </td>
<td>Submitted By: <a href=\"mailto:$email\">$name </td>
<td>Date: $time </td>
</tr>
</table></font><p>";
}
else
{
print "<p><font color=white><table width=\"100%\" bgcolor=#006699 border=1 border=0 cellspacing=0 cellpadding=0 bordercolor=#000000>
<tr>
<td colspan=3><a href=\"$link\">$title</a></td>
</tr>
<tr>
<td colspan=3>Link: <input name=title type=text id=title size=50 value=$link> <p><div align=left>$description</div></td>
</tr>
<tr>
<td>Category: Stories </td>
<td>Submitted By: <a href=\"mailto:$email\">$name </td>
<td>Date: $time </td>
</tr>
</table></font><p>";

}

}


if ($page == 1) // this is the first page - there is no previous page
echo "Previous";
else // not the first page, link to the previous page
echo "<a href=\"stories.php?page=" . ($page - 1) . "\">Previous</a>";

for ($i = 1; $i <= $pager->numPages; $i++) {
echo " | ";
if ($i == $pager->page)
echo "Page $i |";
else
echo "<a href=\"stories.php?page=$i\">Page $i</a>";
}

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);

?>

tree-host
10-07-2005, 12:19 AM
Try this....


<?php
class Pager
{
function getPagerData($numHits, $limit, $page)
{
$numHits = (int) $numHits;
$limit = max((int) $limit, 1);
$page = (int) $page;
$numPages = ceil($numHits / $limit);

$page = max($page, 1);
$page = min($page, $numPages);

$offset = ($page - 1) * $limit;

$ret = new stdClass;

$ret->offset = $offset;
$ret->limit = $limit;
$ret->numPages = $numPages;
$ret->page = $page;

return $ret;
}
}
?>
<?php


$page = $_GET['page'];
$limit = 20;
$result = mysql_query("select count(*) from submissions");
$total = mysql_result($result, 0, 0);

// work out the pager values
$pager = Pager::getPagerData($total, $limit, $page);
$offset = $pager->offset;
$limit = $pager->limit;
$page = $pager->page;

// create query
$query = "SELECT title, description, file, category, link, name, email, time, vidimg, id FROM submissions WHERE cat_id =3 order by id DESC limit $offset, $limit ";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

if ($page == 1) // this is the first page - there is no previous page
echo "Previous";
else // not the first page, link to the previous page
echo "<a href=\"stories.php?page=" . ($page - 1) . "\">Previous</a>";

for ($i = 1; $i <= $pager->numPages; $i++) {
echo " | ";
if ($i == $pager->page)
echo "Page $i |";
else
echo "<a href=\"stories.php?page=$i\">Page $i</a>";
}

//display data
while ($row = mysql_fetch_assoc($result)) {

$cat_id = $row['cat_id'];
$title = preg_replace('/[^\w\s^.^,]/', '',$row['title']);
$title=stripslashes($row['title']);
$file = $row['file'];
$time = $row['time'];
@unset($description);
$description=$row['description'];
$description= preg_replace('/[^ws^.^,]/', '',$description);
$description= nl2br($description);
$description= nl2br($description);
$description=stripslashes($description);
$description = preg_replace( "`((http)+(s)?:(//)|(www\.))((\w|\.|\-|_)+)(/)?(\S+)?`i", "<a href=\"http\\3://\\5\\6\\8\\9\" title=\"\\0\">\\5\\6</a>", $description);
$name = $row['name'];
$email = $row['email'];
$link = $row['link'];
$id = $row['id'];

if ( $row['link'] == '' )
{
print "<p><font color=white><table width=\"100%\" bgcolor=#006699 border=1 border=0 cellspacing=0 cellpadding=0 bordercolor=#000000>
<tr>
<td colspan=3><a href=\"$file\">$title</a></td>
</tr>
<tr>
<td colspan=3>Link: <input name=title type=text id=title size=50 value=http://www.laughsgalore.com/display.php?id=$id> <p><div align=left>$description</div></td>
</tr>
<tr>
<td>Category: Stories </td>
<td>Submitted By: <a href=\"mailto:$email\">$name </td>
<td>Date: $time </td>
</tr>
</table></font><p>";
}
else
{
print "<p><font color=white><table width=\"100%\" bgcolor=#006699 border=1 border=0 cellspacing=0 cellpadding=0 bordercolor=#000000>
<tr>
<td colspan=3><a href=\"$link\">$title</a></td>
</tr>
<tr>
<td colspan=3>Link: <input name=title type=text id=title size=50 value=$link> <p><div align=left>$description</div></td>
</tr>
<tr>
<td>Category: Stories </td>
<td>Submitted By: <a href=\"mailto:$email\">$name </td>
<td>Date: $time </td>
</tr>
</table></font><p>";

}

}


if ($page == 1) // this is the first page - there is no previous page
echo "Previous";
else // not the first page, link to the previous page
echo "<a href=\"stories.php?page=" . ($page - 1) . "\">Previous</a>";

for ($i = 1; $i <= $pager->numPages; $i++) {
echo " | ";
if ($i == $pager->page)
echo "Page $i |";
else
echo "<a href=\"stories.php?page=$i\">Page $i</a>";
}

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);

?>

markerpower
10-07-2005, 12:28 AM
I get:
Parse error: parse error, unexpected T_UNSET in /home/laughsgl/public_html/stories.php on line 143

Line 143:
@unset($description);

If I take out @, this is printed out instead of the story:

ssss,ws.,,ss.sssws,ss.ssws.sws,sssw,s,ws,ss.s,s.w,sssws,ssssss,,s.w,sswswss.sw,wswsssssw.ws...s.s

Christopher Lee
10-07-2005, 12:39 AM
All I'm doin is wild guessing now...

does line 142 have a semicolon terminator ( ; ) on it?

tree-host
10-07-2005, 12:41 AM
drop that line of code then

I.E.


//@unset....

markerpower
10-07-2005, 08:35 AM
Originally posted by Christopher Lee
All I'm doin is wild guessing now...

does line 142 have a semicolon terminator ( ; ) on it?
No it doesn't.

I dropped that line and still get:
ssss,ws.,,ss.sssws,ss.ssws.sws,sssw,s,ws,ss.s,s.w,sssws,ssssss,,s.w,sswswss.sw,wswsssssw.ws...s.s

Christopher Lee
10-07-2005, 09:59 AM
Alright, time to bring out the kludge...
I see we're trying to transform several different things within the content known as '$description'. I'd say, let's try and get the paragraphs working, and the proper text to output, then add in those features.



//enders, stuff not to add paragraph tags to
$enders = array('paragraph' => 'p&gt;', 'div' => 'v&gt;', 'pre' => 'e&gt;', 'h1' => '1&gt;', 'h2' => '2&gt;', 'h3' => '3&gt;', 'h4' => '4&gt;', 'h5' => '5&gt;', 'h6' => '6&gt;');

$description = '';
$tmp = '';
$tmpAr = array();
$tmp = $row['description'];
$tmpAr = explode("\n", $tmp);
$tmp = '';
foreach($tmpAr as $value){
$value = trim($value);
if($value != '' && !in_array(substr($value,-1,2), $enders) ){
$tmp .= '<p>' . $value . "</p>\n";
}else{
$tmp .= $value;
}
}
$description = html_entity_decode($tmp);


EDIT: I forgot the $enders array. Make sure you have that.

Christopher Lee
10-07-2005, 10:14 AM
No coffee in me. I made a bad assumption.

This code assumes that the record in the dB has had htmlentities() applied to it when it was entered. But I shouldn't have assumed anything, because I don't know what the raw record looks like. If you could post a raw record, unless that compromises some sort of security, in which case, of course, don't. If the htmlentities function wasn't used when entering it into the dB, then perhaps changing the '&gt;' to '>' would work. I can't be sure, though, because I haven't tested it that way before.

markerpower
10-07-2005, 11:42 PM
Ok, well I replaced:
@unset($description);
$description=$row['description'];
$description= preg_replace('/[^ws^.^,]/', '',$description);
$description= nl2br($description);
with the code you posted and now everything shows correctly. Only problem now is when I have a link in my text, the link ends with </p>

The code I am using is:
$description= preg_replace('/\s(\w+:\/\/)(\S+)/', ' <a href="\\1\\2" target="_blank">\\1\\2</a>', $description);

Christopher Lee
10-10-2005, 08:27 PM
Can you post or PM a three-paragraph snippet of the raw database record (not formatted by any function) where this is happening?