Take the 2-minute tour ×

I tried googling this, but no luck. Didn't know how to phrase it, lmfao. But anyway, I have this article:


But it shows the bold, italics etc(which messes up the default CSS for the styled articles, when it should actually just show up here: http://puu.sh/1CDwk (which is good)). What I'm asking is how would I go about trying to not to decode html entities? and to just show default text for the news_content?

This is the code:

<?php echo substr($row['news_content'], 0, 150). "...... <a href=''>(Read More)</a>"; ?>
share|improve this question

closed as too localized by meagar, tereško, Eric, Eric J., ithcy Jan 30 '13 at 1:14


possible duplicate of how to remove html tags in php? –  ithcy Jan 30 '13 at 1:14

4 Answers 4

use strip_tags() function in php

echo substr(strip_tags($row['news_content']), 0, 150). "...... <a href=''>(Read More)</a>";
share|improve this answer
And do this before truncating the length, rather than after - so that the resulting length includes only the text remaining after strip_tags. –  thomasrutter Dec 19 '12 at 6:42
Thanks for that comment, i have updated the code –  Akhilraj N S Dec 19 '12 at 6:45
share|improve this answer
wow. I cannot believe I didn't get that. Thanks man. –  TrippedStackers Dec 19 '12 at 6:39
If you do strip_tags after substr, like this, you may end up with half a tag at the end of the string, which could mess up your HTML. Also, you would probably prefer the HTML tags not to be counted in the 150 character limit, which is another reason to strip_tags before substr. –  thomasrutter Dec 19 '12 at 6:44
updated. Thank you –  Miqdad Ali Dec 19 '12 at 6:45

Don't format the content inline - use a stylesheet for content pages. Then on the main page that shortens all of the posts, use a special stylesheet that ignores the formatting.

share|improve this answer

Prior to truncating the length of the HTML string, you need to convert it back to text.

You can do this with strip_tags, followed by html_entity_decode.

Using html_entity_decode is a good idea since otherwise, truncating the string could cut the string part way through an HTML entity, like &nbsp;.

Doing both before truncating the string is a good idea so that the HTML tags and entities aren't included in the length of the resulting string, and that they cannot be chopped in half accidentally.

Here's an overall example:

function gettextexcerpt($myhtml, $len = 150) {
    $myhtml = html_entity_decode(strip_tags($myhtml), ENT_QUOTES, 'UTF-8');
    return substr($myhtml, 0, $len);

Change UTF-8 if you are using a different character encoding for text.

share|improve this answer
