PHP
You wouldn't think was so hard, but I often come across this when processing textarea input in a contact form, into the email to the client....
A long story cut short... the solution I found was below:
$msg = '<p>Address: <br />'.str_ireplace(array("\r\n",'\r\n'),'<br />', $…
A quick example of a SQL statement with REPLACE
$query = 'SELECT id FROM `user` WHERE email ="'.$email.'" AND REPLACE(tel_1, " ","") = "'.$phone.'" LIMIT 1';
//
// Convert database date to something more user friendly
//
function userFriendlyDbDate($date){
$time = strtotime($date);
return date('d/m/Y H:i', $time);
}
... and the other way round, so converts to database date format...
//
// Convert date to database date format
//
function…
Email validation is something that is needed in a variety of situations, but I’ve always struggled to find a good one…. Well, here are 2!
The first is something I found online somewhere, and has been modified a little…
$email = '[email protected]';
if(preg_match("/^[_a-z0-9-]+(\.[_a-z0-9…
A good logout script is needed in most applications, and here’s the one I use. I have this script in a ‘config’ file that I call at the beginning of every file in the site, so you can logout from any page…
if(isset($_GET['logout'])){ // trigger if ?logout is in the url
$_SESSION = array();
if(…
I often forget the exact syntax of things like this… So here’s an example…
$query1 = 'SELECT * FROM `'.$dbName.'`.`users` WHERE email NOT IN (SELECT email FROM `'.$setupDBName.'`.`users`)';
From Mark Jackson
Also the ‘IN’ and ‘NOT IN’ can be used to specify multiple conditional values – for…
The following SQL query causes an error
SELECT vote_vo_ip, COUNT(*) as times FROM `vote`
LEFT OUTER JOIN `voter` ON vote.vote_vo_id = voter.vo_id
WHERE vote_co_id = 2
AND times > 1
GROUP BY vote_vo_ip
The column ‘times’ has not been created when we run the WHERE clause, so we have to use…
If you get a PHP error ‘Warning: Cannot modify header information – headers already sent by‘ it’s normally down to whitespace before or after the PHP start or stop tags. For example a gap before <?phpwill do it…
If in my case, I’ve just put a ‘tried and tested’ script onto a different server,…
This is something you’re likely to do several times in several different scenarios. The common one for me is generating reports from a database.
I tend to build a $_SESSION array as I run the SQL query that is displayed on the page, and then provide a link to ‘Download CSV’. Then the csv-export…
As the title says
// create a function called for escaping data
function escape_data($data){
$data = ereg_replace("[^A-Za-z0-9]", "",$data);
$data = str_replace('\\','',$data);
$data = strtolower($data);
$data = mysql_escape_string(trim($data));
return $data;
}