Category : "Php Snippets"

Php code snippets and examples

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 ...

Send html emails in php using the mail function

The mail function of php can be used to send not only plain text emails, but html emails too. The documentation page shows how to do that. Here is a easy to use function that does the task and has a form very similar to the mail function. function html_mail($to, $subject, $message, $options) { if(isset($options)) { $headers = "From: " . $options . "<".$options.">" . "rn"; } $headers ...

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 ...

Php : Get name and value of all input tags on a page with DomDocument

The following code snippet will extract all input tag names and values as an associative array, from a given html page. /* Generic function to fetch all input tags (name and value) on a page Useful when writing automatic login bots/scrapers */ function get_input_tags($html) { $post_data = array(); // a new dom object $dom = new DomDocument; //load the html into the object ...

Php : Fetch unread gmail emails using CURL

The following code snippet will fetch all unread gmail emails of a given account in xml(atom) format. It does this by simply fetching the page https://mail.google.com/mail/feed/atom using http authentication curl -u gmail_username --silent "https://mail.google.com/mail/feed/atom" /* To use this code, fill the correct values for the variables $gmail_username, $gmail_password */ function get_gmail($username , $password) { $url = "https://mail.google.com/mail/feed/atom"; $c = curl_init(); $options = array( CURLOPT_HTTPAUTH => CURLAUTH_BASIC , CURLOPT_USERPWD => "$username:$password" , CURLOPT_SSLVERSION => ...

Php : Get links on a page with DomDocument

Scraper scripts often need to extract all links on a given page. This can be done in a number of ways like regex, domdocument etc. Here is simple code snippet to do this using domdocument. /* Function to get all links on a certain url using the DomDocument */ function get_links($link) { //return array $ret = array(); /*** a new dom object ***/ $dom = new domDocument; /*** get the HTML ...

Php function to get the number of twitter followers

The following code snippet will fetch the number of twitter followers of a given twitter username /* This function will fetch the number of twitter followers of a twitter id It works by fetching an xml page for the user's information */ function get_followers($twitter_id) { $xml = file_get_contents('http://twitter.com/users/show.xml?screen_name='.$twitter_id); $s = simplexml_load_string($xml); $follower_count = $s->followers_count; return $follower_count; } /* Enter your twitter id here to test the code */ $twitter_id = 'linux'; echo "$twitter_id has ". get_followers($twitter_id) ...

Php function to add st, nd, rd, th to the end of numbers

Often numbers are to be written with a proper suffix, like 1st, 2nd, 3rd, 15th, 21st and so on. So here is a quick function to do the same for a number. Technique 1 /* This function will add st, nd, rd, th to the end of numbers */ function ordinal($i) { $l = substr($i,-1); $s = substr($i,-2,-1); return (($l==1&&$s==1)||($l==2&&$s==1)||($l==3&&$s==1)||$l>3||$l==0?'th':($l==3?'rd':($l==2?'nd':'st'))); } /* Example usage */ for($i = 0; $i < 100; $i++) { echo $i . ordinal($i). ...

Execute shell commands in PHP

Like any other language php applications often need to execute system commands like they are run from the terminal/console/commandline. Php has multiple functions to do this task. Lets take a look at each of these 1. system "system() is just like the C version of the function in that it executes the given command and outputs the result. The system() call also ...

Quick Tip: Getting the Next and Previous Items or Posts

It is a common requirement in lots of projects to fetch the next and previous items or posts and display a link to them on every item or post page. Getting the previous and next item can be quite tricky especially if they need to be user-specific, although it is very easy. I'll show you how - function nextItem($item_id) { ...

Pages:«123»


Connect with us