Noskiw Posted October 22, 2009 Share Posted October 22, 2009 i was wondering how to add a break in a blog post after a certain amount of characters... any idea on how to do this??? this is the code i'm workig with <?php $connect = mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("blog", $connect) or die(mysql_error()); echo "<h1>Blog</h1>"; $query = mysql_query("SELECT * FROM blog"); if (mysql_num_rows($query) == 0) { echo "<hr />There are no posts yet, make the first in the admin section!"; } else { while ($row = mysql_fetch_assoc($query)) { echo "<hr />"; $title2 = $row['title']; $name = $row['name']; $email = $row['email']; $post = $row['post']; $date = $row['date']; $time = $row['time']; echo "<h3>Title: " . $title2 . "</h3><table width='100%'><tr><td><b>Posted by: " . $name . "(" . $email . ") at " . $time . " on " . $date . "</b></td></tr><tr><td>" . nl2br(strip_tags($post)) . "</td></tr></table>\n"; } } echo "<hr />"; $page = $_GET['p']; if ($page) { $path = "/inc/" . $page . ".php"; if (file_exists($path)) { include ($path); } } echo "Are you an admin? <a href='index.php?p=blog&p=credentials'>Place a post</a>"; ?> Link to comment https://forums.phpfreaks.com/topic/178663-want-to-add-a-break-after-a-certain-amount-of-characters/ Share on other sites More sharing options...
cags Posted October 22, 2009 Share Posted October 22, 2009 If you want to split on an amount of characters regardless of whether that leaves you in the middle of a word, you could just use substr on the variable to grab the first x amount of characters. Link to comment https://forums.phpfreaks.com/topic/178663-want-to-add-a-break-after-a-certain-amount-of-characters/#findComment-942454 Share on other sites More sharing options...
Andy-H Posted October 22, 2009 Share Posted October 22, 2009 i was wondering how to add a break in a blog post after a certain amount of characters... any idea on how to do this??? this is the code i'm workig with <?php $connect = mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("blog", $connect) or die(mysql_error()); echo "<h1>Blog</h1>"; $query = mysql_query("SELECT * FROM blog"); if (mysql_num_rows($query) == 0) { echo "<hr />There are no posts yet, make the first in the admin section!"; } else { while ($row = mysql_fetch_assoc($query)) { echo "<hr />"; $title2 = $row['title']; $name = $row['name']; $email = $row['email']; $post = $row['post']; $words = explode(" ", $post); $limit = 30; $date = $row['date']; $time = $row['time']; $show = ((count($words) - 1) > $limit) ? implode(" ", array_chunk($words, $limit)) . '...' : $post; echo "<h3>Title: " . $title2 . "</h3><table width='100%'><tr><td><b>Posted by: " . $name . "(" . $email . ") at " . $time . " on " . $date . "</b></td></tr><tr><td>" . nl2br(strip_tags($show)) . "</td></tr></table>\n"; } } echo "<hr />"; $page = $_GET['p']; if ($page) { $path = "/inc/" . $page . ".php"; if (file_exists($path)) { include ($path); } } echo "Are you an admin? <a href='index.php?p=blog&p=credentials'>Place a post</a>"; ?> I think that should work, you should also add an option to view the full post, maybe using a get variable, and if it is set you could use this after. //Edit $show = ((count($words) - 1) > $limit) ? implode(" ", array_chunk($words, $limit)) . '...<a href="?showall=true">Reveal Post</a>' : $post; /// if they want to show the full post if ( isset( $_GET['showall']) ) $show = $post; Link to comment https://forums.phpfreaks.com/topic/178663-want-to-add-a-break-after-a-certain-amount-of-characters/#findComment-942486 Share on other sites More sharing options...
Noskiw Posted October 23, 2009 Author Share Posted October 23, 2009 i was wondering how to add a break in a blog post after a certain amount of characters... any idea on how to do this??? this is the code i'm workig with <?php $connect = mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("blog", $connect) or die(mysql_error()); echo "<h1>Blog</h1>"; $query = mysql_query("SELECT * FROM blog"); if (mysql_num_rows($query) == 0) { echo "<hr />There are no posts yet, make the first in the admin section!"; } else { while ($row = mysql_fetch_assoc($query)) { echo "<hr />"; $title2 = $row['title']; $name = $row['name']; $email = $row['email']; $post = $row['post']; $words = explode(" ", $post); $limit = 30; $date = $row['date']; $time = $row['time']; $show = ((count($words) - 1) > $limit) ? implode(" ", array_chunk($words, $limit)) . '...' : $post; echo "<h3>Title: " . $title2 . "</h3><table width='100%'><tr><td><b>Posted by: " . $name . "(" . $email . ") at " . $time . " on " . $date . "</b></td></tr><tr><td>" . nl2br(strip_tags($show)) . "</td></tr></table>\n"; } } echo "<hr />"; $page = $_GET['p']; if ($page) { $path = "/inc/" . $page . ".php"; if (file_exists($path)) { include ($path); } } echo "Are you an admin? <a href='index.php?p=blog&p=credentials'>Place a post</a>"; ?> I think that should work, you should also add an option to view the full post, maybe using a get variable, and if it is set you could use this after. //Edit $show = ((count($words) - 1) > $limit) ? implode(" ", array_chunk($words, $limit)) . '...<a href="?showall=true">Reveal Post</a>' : $post; /// if they want to show the full post if ( isset( $_GET['showall']) ) $show = $post; but where would i place that in the code? Link to comment https://forums.phpfreaks.com/topic/178663-want-to-add-a-break-after-a-certain-amount-of-characters/#findComment-942936 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.