Jump to content

Print certain number of words from an article/blog post


dotbin

Recommended Posts

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>';

 

Link to comment
Share on other sites

...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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)."...";
} 

Link to comment
Share on other sites

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)."...";
} 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.