Hi guys.

Im trying to write a php Read More script for my blog. What I want is to be able to render the first two paragraphs and then give a Read More link for the rest of the article. I've tried doing it myself and failed. If someone can point me towards a tutorial or script then I would be forever grateful.

I 've tried a few but can's get them to work the way i want. Is it possible to get this function to only render 2 paragraphs if there is more than 2?

function substrws( $text, $len=750 ) { 

    if( (strlen($text) > $len) ) { 

        $whitespaceposition = strpos($text," ",$len)-1; 

        if( $whitespaceposition > 0 ) 
            $text = 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; 
} 
Member Avatar for LastMitch

@mtho

I 've tried a few but can's get them to work the way i want. Is it possible to get this function to only render 2 paragraphs if there is more than 2?

Did you write this script? I feel you didn't write this script. You just want to modify it but I don't see any errors meaning any effort of trying to make it work! It's so clean.

I'm a bit confused you mention that you just try to write PHP code but the example is in OOP or Classes and Objects. Are you using any PHP mainframe?

I'm just starting to learn OOP on my own.

You can also used AJAX which is in JQuery http://api.jquery.com/jQuery.get/ to render that file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.