Jul
07
2011
0

PHP: Write to a file

The following code writes to a file, it will overwrite the current contents of the file:


$string = "file contents goes here";
$myFile = "file.php";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $string);
fclose($fh);

Make sure you give file.php write access on the server (Apache chmod 775 normally works).

Written by Pete in: php |
Jul
07
2011
0

PHP: Limit a strings characters

This function will limit the number of words shown if the total character limit is over a certain number and add … after the string. Very useful if you want to trim titles or anywhere where you are limited by space:


function limitCharacters($string){
$count = strlen($string);
// change 50 to the number of characters you want to check for
if($count > 50){
//change 5 to the number of words you want to limit to.
//change ... to anything you want to use to show its clipped
$newstring = implode(' ',array_slice(explode(' ',$string),0,5)).'...';
}else{
$newstring = $string;
}
return $newstring;
}

To call the function simply use:

limitCharacters($mystring);

Written by Pete in: php |
Jun
23
2011
0

Find out PHP Version installed from Command Line

At the prompt type:

php -v

Written by Pete in: php |
Sep
20
2010
0

PHP 5 Not Showing Errors

Above your script add in the following line:

ini_set('display_errors', 'On');
?>

Written by Pete in: php |
Jan
05
2009
2

Drupal CCK Autonumber Field

In a Drupal site you may have a content type that needs an auto number to be generated that auto increments on each new node of that type.

To do this install the computed field module

http://drupal.org/project/computed_field

Then create a new field of the type computed field and add the code from the following URL

http://drupal.org/node/313110

Written by Pete in: Drupal,php |
Oct
21
2008
0

Display a Random Image from a Folder

If you need to display a random image from a folder on your website then this tutorial may be of use

http://www.reconn.us/random_file.html

It’s very easy to follow and you can set it up in minutes.

Written by Pete in: php |
Oct
08
2008
0

Increase the memory available to PHP

If you get out of memory errors in PHP or if Drupal suggests you increase the memory available to PHP then you can do this with the following line of code:

ini_set('memory_limit','16M');

In Drupal the best place to put this is in the settings.php file found in sites/default/

Written by Pete in: Drupal,php |

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes