pixeltrace Posted October 24, 2007 Share Posted October 24, 2007 Hi, I have a newsroom section in one of my projects and some of the news title are so long. now, my boss wants to limit the number of characters to be displayed in the lists of news room title to 50 characters only. my problem is, i dont know how to do it. below is my current code for the listing of news title. function getTop3() { $sql = "SELECT * FROM ilec_news WHERE published = 1 ORDER BY news_date DESC LIMIT 0, 3;"; $dbConn = new DBConn(); $con = $dbConn->connect(); $result = mysql_query($sql); $records = array(); while($row = mysql_fetch_array($result)) { $news = new News(); $news->id = $row['news_id']; $news->title = $row['news_title']; $news->content = $row['news_content']; $news->pdf_link = $row['news_pdflink']; $news->date = $row['news_date']; $news->created = $row['news_date_created']; $news->published = $row['published']; $records[] = $news; } mysql_close($con); return $records; } echo '<table width="100%">'; foreach ($records as $row) { echo '<tr><td style="padding-left:5px"><span class="text2"><strong>' . date('d F, Y', $row->date) . '</strong><br>' . $row->title . '</span></td></tr><tr><td><hr class="a" align="left" width="130"></td></tr>'; } echo '</table>'; } hope you could help me with this. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/74551-how-to-limit-number-of-characters/ Share on other sites More sharing options...
corillo181 Posted October 24, 2007 Share Posted October 24, 2007 i got this out of a class you just have to turn it into a function. <?php function shortBio($id,$length){ $query="SELECT bio FROM artist WHERE artist_id='$id'"; $result=$this->db->query($query); $txt = $this->db->fetch_array($result); $newtxt =explode(" ",$txt['bio']); for ($i=0; $i<15; $i++){ $words .= $newtxt[$i].' '; } return stripslashes($words).'...'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74551-how-to-limit-number-of-characters/#findComment-376821 Share on other sites More sharing options...
Toshiba23 Posted October 24, 2007 Share Posted October 24, 2007 use the php substr function $news->title = substr($row['news_title'], 50, 0); Quote Link to comment https://forums.phpfreaks.com/topic/74551-how-to-limit-number-of-characters/#findComment-376824 Share on other sites More sharing options...
clearstatcache Posted October 24, 2007 Share Posted October 24, 2007 Toshiba23 i guess its: $news->title = substr($row['news_title'], 0, 50); not .... $news->title = substr($row['news_title'], 50, 0); to get the first 50 characters of the string.... Quote Link to comment https://forums.phpfreaks.com/topic/74551-how-to-limit-number-of-characters/#findComment-376873 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.