Hi Guys, My 2nd problem I am facing with a new site I'm working on is closing html tags are being truncated, which then results in the remainder of the page following the truncated article, being all bold (strong) or italic (em). Now I know I could prevent this simply by stripping all tags on the home / index page, but if possible I would like to avoid this for SEO reasons. Anyway the code that is doing the truncating of the posts is - # Limit Post function the_content_limit($max_char, $more_link_text = '', $stripteaser = 0, $more_file = '') { $content = get_the_content($more_link_text, $stripteaser, $more_file); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); $content = strip_tags($content, '<h1>,<h2>,<h3>,<strong>,<em>, '); if (strlen($_GET['p']) > 0) { echo ""; echo $content; echo " <a href='"; the_permalink(); echo "'>"."...Read More On "; the_title(); echo "</a>"; echo ""; } PHP: And I was wondering if anyone could tell or show me what to do to get this piece of code to close any open html tags when it truncates the post..? Thanks again Steve
this should give you a good start: <?php $test = "<u>Hi this is a test document it is <b>just a test no need</b> to get all <i>bent out of shape<"; echo get_closing_tags($test); function get_closing_tags($data){ $data = strtolower($data); $tag_opener = array("<b>","<i>","<u>"); $tag_closer = array("</b>","</i>","</u>"); $closers = ""; for($i=0;$i<count($tag_opener);$i++){ //loop through the different tags $opens = substr_count($data,$tag_opener[$i]); $closes = substr_count($data,$tag_closer[$i]); if($opens > $closes){ for($j=0;$j<($opens-$closes);$j++){ $closers = $closers.$tag_closer[$i]; } } } return $closers; } ?> PHP:
Hey thanks a bunch for this, just one question though, as my PHP coding is non existant. Where you have $test as the string variable, could I change this to $content which is the variable used during truncating and then insert the code into the block of code that does the truncating..? Shown below; # Limit Post function the_content_limit($max_char, $more_link_text = '', $stripteaser = 0, $more_file = '') { $content = get_the_content($more_link_text, $stripteaser, $more_file); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); $content = strip_tags($content, '<h1>,<h2>,<h3>,<strong>,<em>, '); if (strlen($_GET['p']) > 0) { echo ""; echo $content; echo " <a href='"; the_permalink(); echo "'>"."...Read More On "; the_title(); echo "</a>"; echo ""; } PHP: Thanks Steve
try this function function mb_substrws( $text, $len=180 ) { if( (mb_strlen($text) > $len) ) { $whitespaceposition = mb_strpos($text," ",$len)-1; if( $whitespaceposition > 0 ) { $chars = count_chars(mb_substr($text, 0, ($whitespaceposition+1)), 1); if ($chars[ord('<')] > $chars[ord('>')]) $whitespaceposition = mb_strpos($text,">",$whitespaceposition)-1; $text = mb_substr($text, 0, ($whitespaceposition+1)); } // close unclosed html tags if( preg_match_all("|<([a-zA-Z]+)|",$text,$aBuffer) ) { if( !empty($aBuffer[1]) ) { preg_match_all("|</([a-zA-Z]+)>|",$text,$aBuffer2); if( count($aBuffer[1]) != count($aBuffer2[1]) ) { foreach( $aBuffer[1] as $index => $tag ) { if( empty($aBuffer2[1][$index]) || $aBuffer2[1][$index] != $tag) $text .= '</'.$tag.'>'; } } } } } return $text; } PHP:
you can use the function like this: $content = $content.get_closing_tags($content); as it outputs the closing tags you just need to add the tags you want to check for to the two arrays inside the function: $tag_opener = array("<b>","<i>","<u>"); $tag_closer = array("</b>","</i>","</u>");
Guys thanks a bunch for this, I'm going to set up a test blog to try all this out cos as I said my coding is non existant and I don't want to screw up my site obviously, but I will get back to you in a couple of days when I've tried out both methods! @G3n3s!s, what exactly is the $len=180 ? I understand the $len is a variable, but Idon't understand where you get the 180 from..? As there could be an open html tag anywhere in the $content which is limited to 400, should I change that 180 to 400..? Thanks again guys you been a great help
$len = 180. if you call that function and you don't define second argument $good_text = mb_substrws($text); it automatically takes that you want max lenght 180 this function cuts text when it reaches 180 chars & closes tags