Archive for the ‘ PHP ’ Category

PHP function to remove whitespaces from a string

Usage :

$spaceRemovedString = removeWhiteSpaces($stringWithSpace);

 

Function :

function removeWhiteSpaces($spaceStr)
{
	$newStr = preg_replace('/\s+/', '', $spaceStr);
	return $newStr;
}

Function to write a message to a text file

Sometimes , we need to write some logging information to a text file in php. The following function takes  filename and message as the parameter and logs whatever you pass to the file with date and time.

function logToFile($fileName, $logMessage)
{
    $logDirectory = “/path/to/log/dir”;     
    $fileHandler = fopen($logDir . '/' . $fileName , "a");
    $logText = date("Y-m-d h:i:s") . "---" . $logMessage . "\r\n";
    fwrite($fileHandler, $logText);
    fclose($fileHandler);
}