Services

  • Drupal CMS Developer
  • Website speed optimisation
  • Drupal 6 to Drupal 8 migrations

Welcome

Welcome, I am a Web Developer based in Madrid, Spain originally from the UK. I studied Computer Science & eBusiness at Loughborough University. I specialise in Content Management System websites

PHP

Replace string in a table

If you want to change certain rows in a database where a query applies … for example an “if” query finding all entries which contain “samplesearchtext”… then run the following script either via mysql/phpmyadmin/php

<?php
mysql_query
("update mytable set myfield =replace (myfield, 'samplesearchtext', 'samplereplacetext')");
?>

Shorten string length

<?php
// substr(input,start point,length)
$query = substr($query,0,-1);
?>

Shortens the string by one character. Change the second value to remove more or see php.net/substr for more information

PHP Listing files in a folder

This script goes through a given folder and finds all of the files and folders within. Using this script it is possible to recursively open up folders.

<?php
$PATH
= $_ENV['DOCUMENT_ROOT'];

$d=$PATH.'/home/server/public_html/folder/';

$files=array(); #initialize array

$dir = opendir($d);
while (
$f = readdir($dir)) {
if (
eregi("\.php",$f)){ #if filename matches .txt in the name
array_push($files,"$f"); #push into $files array
}
}
$count = count($files);
print
":".$d;
for(
$x=0;$x&lt;$count;$x++){

print
"
$x: ".$files[$x];
}
?>

Dynamically read $_POST variables into an array

The code below has been specifically written to take all POST variables and turn them into a string so that they can be used in a URL instead (GET variables)

<?php
foreach ($_POST as $key=>$value)
 
$url .= "&".$key."=" . $value;
?>

Quick and simple Mysql connect

This is the simplest and neatest way to connect to a MySQL database using php

$dbh=mysql_connect ("localhost", "username", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("database_name");

Simple php email script

This is a simple email script which once uploaded, will send an email everytime the page is requested. It may be a good idea to add some security to this by restricting the destination email to stop spam


<?php
$subject = ''
$destemail = ''
$fromemail = ''
$emailtext = ''

$headers = "From: ".$fromemail." \n";
$headers .= "Reply-To: ".$destemail."\n";
$headers .= "MIME-version: 1.0\n";
$headers .= "Content-type: multipart/alternative; boundary=\"Message-Boundary\"\n\n";
$headers .= "This is a multi-part message in MIME format.\n\n";

Reading in all POST and GET variables

This simple one liner will read in all of the variables sent via POST and GET and turn them into relevant variables within this new page, so $_GET[variable] becomes $variable automatically.

@extract($_POST);
@extract($_GET);

If this doesnt work, and some server setups stop your from doing the above, then you can use a variable variable (yes thats right, you did read it correctly) with the following code

<?php
foreach ($_POST as $key => $value)
$$key = $value;

Syndicate content