/    Sign up×
Community /Pin to ProfileBookmark

wordwrap with substr()

Hi all,
I’m having a problem that I just don’t get. On the main page of my site, I have a content area that updates as people post in the forum located within the site. I have a preview of those posts, and am using substr() to cut the posts to 40 characters. For some reason, the strings won’t wrap, and therefore extend the width of the container. Can you take a quick peek at the code and tell me if something jumps out? Thanks,
pj

edit: if I change the substr() to cut at 30 chars, it looks right, but i’d like to have it show something more like 150 chars…

PHP CODE:

[code=php]
<!–Start Recent Post code–>
<div class=”headerPosts”>Recent Posts</div>
<?php

//Connect to the server
$connid = mysql_connect (**** , **** , ****);

//Select the database
$dbSelect = mysql_select_db (****);

//Select headlines
$result = mysql_query(“SELECT * FROM smf_messages ORDER BY posterTime DESC”);

//setting counter variable
$counter = 0;

//Open table
echo ‘<div class=”contentPane”><table border=”0″>’;

while(($row = mysql_fetch_array($result)) & ($counter<3))
{
echo ‘<tr><td class=”recentPosts”>’;

//Subject & author
echo ‘<h3>’.$row[‘subject’].'<br /><small>by ‘.$row[‘posterName’].'</small></h3>’;

//body
if((strlen($row[‘body’]))>40)
{
echo ‘<p>’.substr($row[‘body’],0,40).’…</p><a href=”http://hookedonwinter.com/forum/index.php?topic=’.$row[‘ID_TOPIC’].’.msg’.$row[‘ID_MSG’].’#msg’.$row[‘ID_MSG’].'”><small>go to msg</small></a><br />’;
}
else
{
echo ‘<p>’.$row[‘body’].'<br /><a href=”http://hookedonwinter.com/forum/index.php?topic=’.$row[‘ID_TOPIC’].’.msg’.$row[‘ID_MSG’].’#msg’.$row[‘ID_MSG’].'”><small>go to msg</small></a></p>’;
}

//board name
$boardID = $row[‘ID_BOARD’];
$boardName = mysql_query(“SELECT * FROM smf_boards WHERE ID_BOARD=$boardID”);
while($row = mysql_fetch_array($boardName))
{
echo ‘<small>on board: ‘.$row[‘name’].'</small>’;
}

echo ‘</td></tr>’;
$counter++;
}

//ID_BOARD (smf_boards, name)

//close table
echo ‘</table></div>’;

//close server
mysql_close($connid);

?>
<!–End Recent Post code –>
</div>[/code]

CSS CODE:

[code=html]/* CSS Document */

body {
background-color:#8f9fa3;
font-family:”palatino Linotype”, “bookman Old Style”, “times New Roman”;

/*old colors
#666666 – grayish
#006699 – blueish
#196A90 – gray/blue
#303366 – blueish goodness
#819cae – not for bg, but good
#454954 – possibly best so far. dark grey/blue
*/
}

a:link {
color: #CCCCCC;
text-decoration: underline;
}

a:visited {
color: #AAAAAA;
text-decoration: none;
}

a:hover {
color: #454954;
}

.hereIAm{
text-transform:uppercase;
}

.back{
background-repeat:no-repeat;
background-position:center;
}

/*for now, each header and pane will have to have it’s own class*/

.headerNews {
background-color:#99bbbb;
font-weight:bold;
font-size:18px;
width:205px;
padding-left:10px;
padding-right:10px;
text-align:center;
border:solid thin #000000;
}

.headerPosts{
background-color:#99bbbb;
font-weight:bold;
font-size:18px;
width:206px;
padding-left:10px;
padding-right:10px;
text-align:center;
border:solid thin #000000;
}

.headerWiki{
background-color:#99bbbb;
font-weight:bold;
font-size:18px;
width:205px;
padding-left:10px;
padding-right:10px;
text-align:center;
border:solid thin #000000;
margin-left:5px;
}

.contentPane{
background:#668899;
color:#CCCCCC;
border:solid thin #000000;
margin-top:5px;
}

.recentNews{
font-size:10px;
width:200px;
padding-left:10px;
padding-right:10px;
float:left;
margin-top:5px;
}

.recentPosts{
font-size:12px;
width:200px;
padding-left:10px;
padding-right:10px;
margin-top:5px;
text-align:justify;
}

.recentChanges{
font-size:12px;
width:227px;
padding-bottom:5px;
padding-left:5px;
margin-top:5px;
}

.newPages{
font-size:12px;
width:227px;
padding-bottom:5px;
padding-left:5px;
margin-top:5px;
clear:left;
}

.mainContent{
color:#BBB;
background-color:#303366;
font-size:14px;
font-weight:bold;
padding-left:10px;
padding-right:10px;
padding-top:.5px;
padding-bottom:.5px;
}[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@bokehNov 08.2006 — [code=php] if((strlen($row['body']))>40)
{
echo '<p>'.substr($row['body'],0,40).'...</p><a href="http://hookedonwinter.com/forum/index.php?topic='.$row['ID_TOPIC'].'.msg'.$row['ID_MSG'].'#msg'.$row['ID_MSG'].'"><small>go to msg</small></a><br />';
}
else
{
echo '<p>'.$row['body'].'<br /><a href="http://hookedonwinter.com/forum/index.php?topic='.$row['ID_TOPIC'].'.msg'.$row['ID_MSG'].'#msg'.$row['ID_MSG'].'"><small>go to msg</small></a></p>';
}[/code]
[/QUOTE]
That's unnecessary. All you need is:[code=php]echo '<p>'.substr($row['body'],0,40).'...</p><a href="http://hookedonwinter.com/forum/index.php?topic='.$row['ID_TOPIC'].'.msg'.$row['ID_MSG'].'#msg'.$row['ID_MSG'].'"><small>go to msg</small></a><br />';[/code]No if/else needed. As for the problem, give your paragraph an ID and style it with CSS.
Copy linkTweet thisAlerts:
@HookedOnWinterauthorNov 08.2006 — yah, that's a good point. I had the if/then because originally i wanted to cut posts at 150 chars or something like that, but i guess the addition of a ... is ok.

i'll try it out and get back to you. Thanks!
Copy linkTweet thisAlerts:
@HookedOnWinterauthorNov 08.2006 — All set. Thanks for your help!
×

Success!

Help @HookedOnWinter spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...