1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Restoring & closing truncated html tags..?

Discussion in 'PHP' started by Taffy1957, Jan 21, 2011.

  1. #1
    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 "&nbsp;<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
     
    Taffy1957, Jan 21, 2011 IP
  2. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #2
    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:
     
    shofstetter, Jan 22, 2011 IP
    darklift likes this.
  3. Taffy1957

    Taffy1957 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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(']]>', ']]&gt;', $content);    
    $content = strip_tags($content, '<h1>,<h2>,<h3>,<strong>,<em>, ');   
    if (strlen($_GET['p']) > 0) {      echo "";      
    echo $content;      
    echo "&nbsp;<a href='"; the_permalink();      
    echo "'>"."...Read More On ";
    the_title();      
    echo "</a>";      
    echo "";     }
    PHP:
    Thanks Steve
     
    Taffy1957, Jan 22, 2011 IP
  4. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #4
    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:
     
    G3n3s!s, Jan 22, 2011 IP
  5. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #5
    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>");
     
    shofstetter, Jan 22, 2011 IP
  6. Taffy1957

    Taffy1957 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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
     
    Taffy1957, Jan 23, 2011 IP
  7. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #7
    $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
     
    G3n3s!s, Jan 23, 2011 IP