dotbin Posted March 16, 2011 Share Posted March 16, 2011 Hi all - I'm setting up a custom PHP blog. It pulls the data from a MySQL database which includes HTML tags (<p><div><span> etc...). I would like to display only up to 50 words per post on the blog page, which users can then read and click a link to then see the entire post. I've developed some code which does this, however, it seems to be stripping my HTML tags... Very sad! Would be very very grateful if one (or more) of you kind lot would have a look at my code and let me know if there is a easier (I'm all for easy) and proper way of implementing this so that it works without stripping my HTML tags. Cheers!!! // Counts number of blog words in the content $blog_content_words = str_word_count($blog["content"], 1); // Sets the blog_words variable to 0 $blog_words = 0; // Prints title on page as a permalink to a post echo '<h1><a href="blog.php?id='.$blog["id"].'">'.$blog["title"].'</a></h1><p>'; // Loops while blog_word is under 50 while($blog_words < 50) { // Prints a word from blog_content_words array of blog post and adds a space afterwards echo $blog_content_words["$blog_words"]." "; // Adds 1 to the blog_words counter ++$blog_words; } // Adds a read more link to the post which links to full blog post echo '... <a href="blog.php?id='.$blog["id"].'">[read more]</a></p>'; Quote Link to comment https://forums.phpfreaks.com/topic/230818-print-certain-number-of-words-from-an-articleblog-post/ Share on other sites More sharing options...
ManiacDan Posted March 16, 2011 Share Posted March 16, 2011 ...and this is why we recommend not storing HTML in a database. You run into this very problem. What you might want to do is: strip the HTML find the 49th and 50th word. use a regular expression to trim the original article down to the content up to those two words (which may be separated by one or more HTML tags) Print the resulting string, with any weird tags removed. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/230818-print-certain-number-of-words-from-an-articleblog-post/#findComment-1188239 Share on other sites More sharing options...
dotbin Posted March 16, 2011 Author Share Posted March 16, 2011 I know, I know - but here's the problem - what if I want to include an image in my blog post - it's easy to just write the <a href> link into the database text. Know what I mean? Quote Link to comment https://forums.phpfreaks.com/topic/230818-print-certain-number-of-words-from-an-articleblog-post/#findComment-1188240 Share on other sites More sharing options...
dotbin Posted March 16, 2011 Author Share Posted March 16, 2011 By the way - Cheers I'll take a look at this... ...and this is why we recommend not storing HTML in a database. You run into this very problem. What you might want to do is: strip the HTML find the 49th and 50th word. use a regular expression to trim the original article down to the content up to those two words (which may be separated by one or more HTML tags) Print the resulting string, with any weird tags removed. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/230818-print-certain-number-of-words-from-an-articleblog-post/#findComment-1188241 Share on other sites More sharing options...
jamesjmann Posted March 16, 2011 Share Posted March 16, 2011 Like Maniac Dan said, you should use regular expressions for stripping the html tags, and then use substr() for limiting the number of characters you want displayed. Nine times out of ten, you won't need user-submitted html for displaying multiple db results. The html would probably only need to be used when the full blog entry is being displayed. So if you're displaying multiple blog entries with a title and a small summary of the body text, you would use regex and substr, and if you're displaying each blog individually, you would not need the substr, and you would oust the regex completely, so that the html submitted by the user would be sent to the browser to construct the page. Quote Link to comment https://forums.phpfreaks.com/topic/230818-print-certain-number-of-words-from-an-articleblog-post/#findComment-1188244 Share on other sites More sharing options...
dotbin Posted March 16, 2011 Author Share Posted March 16, 2011 Thanks James. I truly have not idea what I'm doing. Quote Link to comment https://forums.phpfreaks.com/topic/230818-print-certain-number-of-words-from-an-articleblog-post/#findComment-1188251 Share on other sites More sharing options...
dotbin Posted March 16, 2011 Author Share Posted March 16, 2011 Anyone got any more ideas. I'm on the lower tiers of PHP knowledge. Quote Link to comment https://forums.phpfreaks.com/topic/230818-print-certain-number-of-words-from-an-articleblog-post/#findComment-1188292 Share on other sites More sharing options...
phpTrainee Posted March 16, 2011 Share Posted March 16, 2011 Try this. It strips html tags, finds a space after the number of characters and then finds the last word. $message = strip_tags($message); if (strlen($message) > 200) { $space = strpos($message," ",200); $lastword = $space -1; $message = substr($message, 0, $lastword)."..."; } Quote Link to comment https://forums.phpfreaks.com/topic/230818-print-certain-number-of-words-from-an-articleblog-post/#findComment-1188355 Share on other sites More sharing options...
jamesjmann Posted March 16, 2011 Share Posted March 16, 2011 Thanks James. I truly have not idea what I'm doing. Sorry, just trying to help; I guess I misunderstood your problem. Quote Link to comment https://forums.phpfreaks.com/topic/230818-print-certain-number-of-words-from-an-articleblog-post/#findComment-1188444 Share on other sites More sharing options...
dotbin Posted March 17, 2011 Author Share Posted March 17, 2011 This looks excellent - cheers! will give it a shot. Try this. It strips html tags, finds a space after the number of characters and then finds the last word. $message = strip_tags($message); if (strlen($message) > 200) { $space = strpos($message," ",200); $lastword = $space -1; $message = substr($message, 0, $lastword)."..."; } Quote Link to comment https://forums.phpfreaks.com/topic/230818-print-certain-number-of-words-from-an-articleblog-post/#findComment-1188599 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.