Saturday, April 19, 2014
Text Size

PHP: First name and Initial of Surname

A note for myself on some code to convert a string of two names into a string made up of the first name and then using the initial of the second name.

  1.  -- What I have 
  2.  John Smith 
  3.  Fred.Bloggs 
  5.  -- What I want 
  6.  John S. 
  7.  Fred B. 


Anti-SQL Injection Function - Not a silver bullet

So, seriously, this is not going to stop all the attacks out there; it is simply a base function to build upon. This is a PHP function I got off the PHP forums years ago, and can't find it anymore, so I'm posting it here... And now people can see how I safeguard my sites but obscurity should never be a recommended security measure.

This function will check a posted string for suspicious activity and email the specified system administrator. You should modify it to suit your sites needs (eg. if you need to accept url encoded values). It needs some work I know but it's a start. I'd advise using PHP sessions though as well (see after).

Preg_Replace all strings between two tags

For those of you who use Preg_Replace. Preg_replace is a function that uses regular expressions to search and replace a string.

Because my understanding with regular expressions is shady and varies from language to language, I've written this article as a quick reference point.


Php convert filesizes to bytes kb mb gb

Just a quick note on how to format a given filesize and to reduce the display output to a small string, eg:

  1.  196 bytes          : displays as => "196 bytes" 
  2.     12945 bytes        : displays as => "12 Kb" 
  3.     1478515 bytes      : displays as => "1 Mb" 
  4.     8798745455 bytes   : displays as => "8 Gb" 

Convert seconds to total time in PHP

This is intended for activity/session durations and although I usually get MySQL to do the date/time calculations, there are times when we have to do with PHP. This is the shortest way I know to properly convert seconds into total hours, minutes and seconds (taking into account regional settings and without using a date function).


  1.  $total_time =intval(intval($total_seconds)/ 3600).":"; 
  2.  $total_time.=str_pad(intval(($total_seconds/60)%60),2,"0",STR_PAD_LEFT).":"; 
  3.  $total_time.=str_pad(intval($total_seconds%60),2,"0",STR_PAD_LEFT); 
  5.  // yields 
  6.  // 82800  = 23:00:00 
  7.  // 108000 = 30:00:00 
On one line:

  1.  $total_time=intval(intval($total_seconds)/ 3600).":".str_pad(intval(($total_seconds/60)%60),2,"0",STR_PAD_LEFT).":".str_pad(intval($total_seconds%60),2,"0",STR_PAD_LEFT); 

Latest Posts

  • 301 Redirect using htaccess file

    • Tue 15-Apr-14
      Further suggestion:
      RewriteRule ^(.*/)?assets/s 2dmain.html\?/( .*/)? $2 [R=301,L]
    • Thu 10-Apr-14
      Playing with some RegEx testers
      RewriteRule ^(.*\?/)?(?:$|( .+?)(?:(\.[^.]$ )|$)) $2 [R=301,L] ...
    • Wed 09-Apr-14
      I tried to redirect links from my old site to my new site, based on what I read in this thread. The old ...
  • JComments 2.3.0 with ReCaptcha in Joomla 2.5.x

    • Fri 28-Mar-14
      You are a rockstar mate! thanks. Followed the steps listed and it worked! If only all tutorials were ...
  • K2 Items disappear

    • Thu 03-Apr-14
      The fix works great, but the problem is occurring a couple of times a day. Any idea how to fix ...
      Larry C.