Jump to content

PHP read more help...


jpopuk

Recommended Posts

Hi,

 

I am working on a project that I thought would be simple.

 

Issue I am having is I need the content to display in HTML format in a post, which I have done. But I also need to limit the characters, and then a read more link will appear.

 

This is the code I use to view the content which works fine:

<?php echo check_output($post['content'], true, false, false); ?>

This is how I am doing it though so it only shows so many characters:

<?php $mylink = get_url('/blog/', $post['category_id'], $post['post_id'], $post['title']); echo strlen($post['content']) > 250 ? substr($post['content'], 0, 250) . '...<p align="right"><strong><a href="'. $mylink .'">read more</a></strong></p>' : $post['content']; ?>

Issue I am having is the content is being displayed as text so it is showing <p> tags etc.

 

If anybody could assist on merging the 2 so the content displayed shows HTML would be very much appreciated.

 

This is the check_output fuction:

function check_output($output, $allow_html = false) {
	if ($allow_html == true) {
		$output = html_entity_decode($output, ENT_QUOTES);
	}

	return $output;
}

Thanks

 

Link to comment
Share on other sites

Why are you passing four parameters to the check_output(0 function which only accepts two?

 

The problem that you are going to have is that if you are going to limit the comments to a certain length, you will end up with some invalid HTML code because some opening HTML tags may come after the point where you are going to limit the length. This is not a trivial problem to solve. I'd suggest some google searching to find a solution that someone has already built.

 

This seems to include some good resources: https://www.google.com/#q=php+truncate+text+retain+HTML

Edited by Psycho
Link to comment
Share on other sites

  • 2 weeks later...

Thanks for reply. What you said made good sense with regards to only allowing a certain length as HTML would be invalid.

 

I did a simple solution and put the HTML in a div with overflow set to scroll. - Not the most ideal way but it looks fine!

 

Again thanks.

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.