Skip to main content

Posts

Showing posts with the label PHP

How to fix a PHP Fatal error: Allowed memory size of XXXXX bytes exhausted

If you're working with a PHP website, Magento in my case, and you get a page that is half blank when loading it's possible that you're experiencing a fatal PHP error.  This quick post will show you how to fix this in Magento 1.6.1.0. Overview Verify that you're getting a fatal PHP error related to memory size Update your .htaccess file Update your php.ini configuration Step 1:   Verify the error To verify that you are experiencing a fatal PHP error related to the memory size available you must first find your PHP error log file.  My host provides this through cPanel but if you're using your own Apache server it can usually be found in /var/logs/apache2/errors.log (see below). Open using whatever editor you like (geany in my case), when opened, I was seeing this: Apache2 error.log file showing a PHP Fatal error My log was giving this error: [23-Nov-2011 17:13:12] PHP Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to all

Creating a snippet or excerpt of words from a text body in PHP

This is a little helpful function I wrote in PHP to make a content snippet that tries to prevent cutting off halfway through a word.  It keeps the number of words to a given amount and also restricts the text to a specfic length if the words go over the character limit. It is pretty simple but works great! function excerpt($text, $words=12, $end='...',$limit=120) {     // split the string by spaces into an array     $split = explode(' ',$text);     if (count($split)>$words) {         // rebuild the excerpt back into a string         $text = join(' ',array_slice($split,0,$words));     }     // append the ending, limit it, and return     return substr($text,0,$limit-length($end)).$end; } There you go, happy coding!