Tag: PHP/MYSQL

PHP database logging

In order to make database logging in php we should first create a table with 2 fields: the message that we log and the date that the log was made. After creating the database we should create a php class that will have to pass the instance of the MySQL database to the log class contructor. After that as previous example of php log file we should call a function that inserts a new row in a table database. Here is the class for php database logging:


class DBLog {
private $_db;

public function __construct($db) {
$this->_db = $db;
}

public function WriteLogToDB($message) {
if(!empty($message)){
$date = date("Y-m-d h:m:s");
mysql_query ("INSERT INTO DBLog VALUES('".$message."', '$date')", $this->_db);
}
}
}

In order to call the function of database logging we should first make a connection to the database and then call the function to save row log to database:


$connection = mysql_connect("localhost", "username", "password")
or die("Unable to connect to MySQL server");
mysql_select_db('DatabaseName', $connection) or die ("Database not found.");
$filel = new DBLog($connection);
$filel->WriteLogToDB('Here is my first log.');

We only need the mysql script that creates the table in our database:


CREATE TABLE IF NOT EXISTS `dblog` (
`Message` text NOT NULL,
`DateCreated` datetime NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Tags: ,

PHP string manipulation

This is a first part of PHP string manipulation.
It’s extremelly useful to know how to manipulate strings in PHP. This tutorial will guide you through how to use the functions that PHP offers.
A string in PHP it’s defined in php in a variable assigning a single or double quote value like:
$string = ‘This is a test string’;
$string = “This is a test string”;

Make all letters from string lowercase

To make every letter of a string in lowercase you should use the function strtolower like this:

<?
$string= "This is a  strtolower Function Test";
echo strtolower($string);
?>

The ouput will be: this is a strtolower function test

Make all letters of a string uppercase

You should use the function strtoupper in order to make every letter of the string in uppercase:

<?
$string= "This is a  strtoupper Function Test";
echo strtoupper($string);
?>

The ouput of will be: THIS IS A STRTOUPPER FUNCTION TEST

Capitalize first letter of each word in a sentence with ucwords

To make a sentence more estetic we can capitalize first letter of each word in a sentence with the function ucwords().

<?
$string= "This is a  ucwords Function Test";
echo ucwords($string);
?> 
Output: This Is A Ucwords Function Test

This function touch only the first letter of each word and leave other characters like they are. We can improve the way how the string is displayed by combining with the strtolower function.

<?
$string= "ThiS is a  ucwords FunctioN Test";
echo ucwords(strtolower($string));
?>

Output will be:This Is A Ucwords Function Test

Remove tabs,spaces,new lines from beginning and end of a string

To remove spaces,tabs and new lines from the beginning and end of a string you should use the function trim(). This function accepts as an input a string, that will be cleaned and returns a string without spaces,tabs and new lines.

<?
$string= "\t \nThis is a  trim Function Test   ";
echo trim($string);
?>

Output: This is a trim Function Test

Remove spaces,tabs and new lines from begining of a string

Similar like trim we can use ltrim to remove spaces,tabs and new lines from the beginning of a string

<?php
$string= "\t \nThis is a  trim Function Test \t\t  ";
echo ltrim($string);
?>

Output: This is a trim Function Test

Remove spaces,tabs and new lines from the end of a string

Similar like ltrim we can use rtrim to remove characters that we don’t need:

<?
$string= "\t \nThis is a  rtrim Function Test \t\t  ";
echo rtrim($string);
?>

Remove tags from a string

To remove tags from a string you should use strip_tags to have a clean string without tags

<?php
$string= "<pre>This is a  trim Function Test</ pre>";
echo strip_tags($string);
?>

PHP string manipulation – part 2

Tags: ,
Back to top