by
Janeth Kent
Date: 11-04-2013
Here’s a mini list of useful PHP code snippets that we find using on a weekly basis. You could use these as is or expand them as part of other applications or add them to a php class.
Adjust server time
Useful if you have a server in a different timezone, like us... You can fix funny things happen when you add the time to a MySQL database, by adding or subtracting hours as in the example below.
$now = date('Y-m-d-G');
$now = strftime("%Y-%m-%d-%H", strtotime("$now -8 hours"));
Convert HEX value to RBG
function hextorgb($hexvalue){
if($hexvalue[0] == '#') {
$hexvalue = substr( $hexvalue, 1);
if(strlen( $hexvalue ) == 6){
list($r, $g, $b) = array($hexvalue[0] .$hexvalue[1], $hexvalue[2] . $hexvalue[3], $hexvalue[4] .$hexvalue[5]);
}elseif (strlen($hexvalue) == 3) {
list($r,$g,$b) = array($hexvalue[0] .$hexvalue[0], $hexvalue[1] . $hexvalue[1], $hexvalue[2] .$hexvalue[2]);
return array('R' => $r, 'G' => $g, 'B' => $b);
$rgb = hextorgb('#fff000');
print_r($rgb);
Display a users Gravatar image
function url_to_link($text){
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// Check if there is a url in the string
if (preg_match_all($reg_exUrl, $text, $url)) {
$position = strpos($text,' '.$v)+1;
$text = substr_replace($text,'',$position, strlen($v));
$text = substr_replace($text,''.$v.'',$position ,0);
// if no urls in the text just return the text
$string = 'This is a string of text and we have a link: http://ma-no.org we also have another link http://google.com';
echo url_to_link($string);
Parse JSON in PHP
$json='{"id":0,"name":"Silvia","surname":"Salvietta","Website":"http://ma-no.org"} ';
$array=json_decode($json);
echo $array->name;
by
Janeth Kent Date:
11-04-2013
hits :
2793
Latest news from Hi-Tech world
PHP never ceases to surprise me with its built-in capabilities. These are a few of the functions I find most fascinating.
1. Levenshtein
This function uses the Levenshtein algorithm to calculate the…
The internet has transformed the way that people live their lives. You can access a wealth of knowledge from a device that fits in your hand. Yes, it’s used for…
If you are one of the 2.5 million sellers on Amazon, you’ll know that the platform has incredible potential for profits. However you’ll also know the competition is fierce and…
As the digital landscape continues to evolve, ecommerce has become an essential part of businesses worldwide. Entrepreneurs, small businesses, and even established enterprises are seeking robust and costeffective solutions to…
There are many people today who only use the following for design purposes Canva as it is a really popular software and website and there is no denying that it…
We have already written several articles about the artificial intelligence that is revolutionising the world, but this time we will talk about how it can help you with everyday tasks…
The best and most amazing Alexa Hacks you should know about
Alexa, Amazon's talking Artificial Intelligence contained in the Echo, can entertain you and your family if you know the right…
Today we know that having a presence on social media is becoming increasingly important if you want to boost your business and reach a wider audience.
But first of all,
What is…
In the ever-evolving landscape of web development, CSS remains an indispensable language for creating stunning and engaging user interfaces. Crafting visually appealing projects with CSS often entails writing extensive lines…
In the world of web design, flat design has gained immense popularity for its clean and minimalist aesthetic. If you're a web designer looking to enhance your projects with sleek…
In today's fast-paced digital world, a sluggish Android device can be a source of frustration. Slow app launches, laggy performance, and delays in responsiveness can hinder your productivity and overall…
Since its inception, the World Wide Web has revolutionized the way we access and interact with information. However, with the rapid advancements in technology and the emergence of new paradigms,…
When embarking on your next application development project, opting for a framework is a smart choice. If you're already well-versed in a particular framework, it's natural to lean towards using…
You recently experienced one of the most epic concerts in recent years and now you're in the process of showing the videos you recorded to friends and family who, unfortunately,…
Google News is a tool, from Google, that spreads current, reliable and truthful content from different websites or portals dedicated exclusively to news. The sites that appear in Google News have…
If you have a strong knowledge in programming and want to improve your JavaScript skills or you want a good reference book then this list is for you.
We've put together…
Welcome here again! Last time we didn't listen to our very first user events and and methods to react to these events. Now we are going to explain directives and…
We will explain, step by step, how you can create Spotify playlists using ChatGPT.
The arrival of ChatGPT, the chatbot based on OpenAI's natural language model, has revolutionized the world of…
In today's digital age, email marketing remains a powerful tool for businesses to connect with their audience. However, as inboxes become increasingly crowded, it is crucial to deliver relevant and…
As technology continues to advance, navigation systems have become an integral part of our daily lives. From GPS-enabled smartphones to in-car navigation systems, we rely on these tools to guide…