Preg_Replace all strings between two tags
- Category: Personal Home Page
- Hits: 45689
For those of you who use Preg_Replace. Preg_replace is a function that uses regular expressions to search and replace a string.
Why?
Because my understanding with regular expressions is shady and varies from language to language, I've written this article as a quick reference point.
How?
Php convert filesizes to bytes kb mb gb
- Category: Personal Home Page
- Hits: 14479
Just a quick note on how to format a given filesize and to reduce the display output to a small string, eg:
196 bytes : displays as => "196 bytes" 12945 bytes : displays as => "12 Kb" 1478515 bytes : displays as => "1 Mb" 8798745455 bytes : displays as => "8 Gb"
Counting the occurence of a word within a string: Benchmark
- Category: Personal Home Page
- Hits: 7468
I added this article because when I was only trying to look up how to do this, this nut "the Storyteller" went and carried out a benchmark test on the most popular ways of counting the occurrence of a specific word within a string of text.
Using a HTML form and PHP to upload a file
- Category: Personal Home Page
- Hits: 10343
Basically you have a HTML form with an input field type of 'FILE' (ie. <input type="file" name="file_to_upload" />) and want a PHP file to process this. This example applies to a Linux Apache MySQL PHP (LAMP) environment.
The Solution
1. The first thing to do is check that your HTML form is setup to do this:
Converting SQL date in PHP to European date format and vice-versa
- Category: Personal Home Page
- Hits: 10266
If you've ever made PHP scripts to process data within a LAMP environment (Linux, Apache, MySQL, PHP) then this happens a lot. In the following example, our HTML form will allow the user to specify a date (so excludes hours, minutes and seconds). For demonstration purposes, I'm going to be using the European date format so DD/MM/YYYY.
The SolutionCreating a Top 10 chart with less code
- Category: Personal Home Page
- Hits: 7229
I've decided to put something in here as it took me an age to find out how I could do it.
This is when using a MySQL query within a PHP script. The process is used often to do a statistics table or top ten chart of your data (eg. movies, music, etc).
My aim is to do the following:
- retrieve data from a table,
- count the number of times each data exists,
- sort it in reverse order so that the most frequent is at the top of the list
- print out each row with the number of times that particular data appeared in a row
My old method was to:
UTF8 Unicode PHP MySQL for International Characters
- Category: Personal Home Page
- Hits: 14674
- Joomla works fine with international characters
- A Module Extension I wrote for Joomla! displayed funny characters.
- The Joomla! articles were displaying the correct characters for that language set.
- I needed to make my extension read data from a MySQL database and display the caracters as intended with UTF8.
I tried enough extensions and forum solutions, and although these changes would have an effect on the module (such as take away accents and convert to ASCII), they weren't what we were looking for.
The quick solution was to make the script run a MySQL command at the start:
SET NAMES 'utf8'Now I need to run this command from within a Joomla! extension using the mysql Joomla! classes, here's how I've used in this script pulling IDs and titles from the a sample table:
Convert seconds to total time in PHP
- Category: Personal Home Page
- Hits: 6584
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).
How?
$total_time =intval(intval($total_seconds)/ 3600).":"; $total_time.=str_pad(intval(($total_seconds/60)%60),2,"0",STR_PAD_LEFT).":"; $total_time.=str_pad(intval($total_seconds%60),2,"0",STR_PAD_LEFT); // yields // 82800 = 23:00:00 // 108000 = 30:00:00On one line:
$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);
Page 2 of 3
Credit where Credit is Due:
Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.
Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.
Thank you for visiting and, as always, we hope this website was of some use to you!
Kind Regards,
Joel Lipman
www.joellipman.com
Latest Articles
Accreditation

