Tag: PHP
-
Calculate time left in day in PHP
Before PHP 5.3, finding the difference between two dates was a bit of a pain. Convert the date strings to UNIX timestamps (if they weren’t already), subtract them, and then divide the result until you had each of the components (seconds, minutes, et cetera). As of PHP 5.3+ you can…
-
Email validation via MX lookup in PHP
Validating that an email addresses ain’t what it used to be. I remember writing simple regular expressions that worked out really well. These days there are a bajillion TLDs with more being added all of the time. Something I started doing is checking that the domain of the email being…
-
Check if a string contains a character with PHP
Checking if a string contains a character could be accomplished easily with a regular expression or you can use the built-in PHP functions. To check if a string contains a character of a specific case you can use strpos. $haystack = ‘This is my haystack that we shall check’ $has_A…
-
Generate random hex color with PHP
There’s many ways to skin a cat, or generate a color. You could randomize a number between 0 and 16777215 then convert it to hex: $rand_color = ‘#’ . dechex(mt_rant(0, 16777215)); Or you could do what I like to do, just md5 a random string and grab the first 6…
-
Run a block of PHP code a certain percentage of the time
Sometimes, you may want to run code all of the time. Other times, you may want to run code some of the time. This is how garbage collection in PHP works. Based on your configuration for gc_probability and gc_divisor, garbage collections runs a fraction of the time (defaulting to 1/100…
-
PHP is ꜱtupid and so are you
I hope you’re really fucking pissed right now. I hope you go thermonuclear on the comments box. Why? Because it really just proves how right I am about how stupid you are. You let some silly blog post get under your skin. Thing is, I really didn’t know what to…
-
Detect bots and spammers with Project Honey Pot and PHP
Project Honey Pot is one of my favorite services. They offer an API that allows you to pull information on an IP address. This comes in very handy when vetting traffic coming to your website. I discovered the service a few years back after realizing that I had some spammers…
-
How to convert a string to a time with PHP
Converting a string to a timestamp is one of my favorite things about PHP. In fact when trying to decide which language to build [HolidayAPI][holidayapi] in I couldn’t find an implementation of PHP’s strtotime() that rivaled the original. The strtotime() function takes a textual string as input and then spits…
-
Set array pointer to a specific key or value in PHP
PHP arrays are absolutely fantastic. They hold stuff, doesn’t really matter what. The interfacing is pretty consistent and there’s a boat load of built-in array functions. What PHP lacks is the ability to set the array pointer to an arbitrary key or value. There are ways to move forward and…
-
Basic page routing in PHP
Last week we talked about setting up a local development server with PHP’s built-in web server and I mentioned that we’d delve into page routing. Routing refers to taking the URI that a person was requested, let’s say /about and routing that to the appropriate code. Sure, you could just…