Browsing articles tagged with

php snippets

Validate domain name using filter_var function in php

The filter_var function of php is capable of validating many things like emails, urls, ip addresses etc. It does not have a direct option to validate a domain name however. So I coded up this little snippet that the filter_var function with a little tweak so that it can validate domain names as well. function filter_var_domain($domain) { if(stripos($domain, 'http://') === 0) { $domain = substr($domain, ...

Download a file using curl in php

Here is a quick curl snippet for php, that can download a remote file and save it. <?php set_time_limit(0); //File to save the contents to $fp = fopen ('files2.tar', 'w+'); $url = "http://localhost/files.tar"; //Here is the file we are downloading, replace spaces with %20 $ch = curl_init(str_replace(" ","%20",$url)); curl_setopt($ch, CURLOPT_TIMEOUT, 50); //give curl the file pointer so that it can write to it curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $data = curl_exec($ch);//get ...

Php function to get the last day of a month

Here is a function that would get the last day of a given month as a date in format Y-m-d. It first adds 1 month to the 1st date of the given month, then subtracts 1 second from the resulting date which brings back to the last date of the given month. /** Last date of a month of a year @param $month ...

Fill text templates using arrays in php

Text templates are often used to generate content like notification emails, invoices filled with the details of a customer or user. The best example would be a bulk email program that sends out emails to multiple users filling the details of the particular user in every individual mail. Text templates have place holders for fields which can be filled using an ...



Connect with us