List all the files in a directory in php
This code will display all the files in the images folder.
foreach (glob("images/*.*") as $filename) { echo basename($filename); }
The following lines of code will provide search results using the google search API.
//search text to be passes $search_text = "katharnavas"; $url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=".$search_text; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $results = curl_exec($ch); curl_close($ch); // The results will be in json encoded format so do a decode $json = json_decode($results); print_r($json);
The following lines of code are used to update your status in twitter.
//Text to be updated on twitter $status = "Hi this is the text to be updated"; //your twitter username $username = "xxxxusername"; //your twitter password $password = "password"; $postvars = "status=".$status; $url = "http://twitter.com/statuses/update.xml";</code> $ch = curl_init($url); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_HEADER, 0); // DO NOT RETURN HTTP HEADERS curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // RETURN THE CONTENTS OF THE CALL $res=curl_exec($ch); curl_close($ch);
The following are the different ways of reading a web page or external file contents in php.
$pageurl = "http://google.com";
//First Method
$r = new HTTPRequest($pageurl); $html = $r->DownloadToString(); echo $html;
//Second Method
$html =file_get_contents($pageurl); echo $html;
//Third Method (Using Curl) For this curl support should be enabled.
echo phpinfo();
//to check whether curl is enabled or not.
$ch = curl_init($url); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // DO NOT RETURN HTTP HEADERS curl_setopt($ch, CURLOPT_HEADER, 0); // RETURN THE CONTENTS OF THE CALL curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $res=curl_exec($ch); curl_close($ch); print_r($res[2]);
This function checks whether the email provided is valid and returns false if not valid.
Php Function for validating email
//$email -> email value to be validated.
function callEmailCheck($email, $strict = false) { $regex = $strict? '/^([.0-9a-z_-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i' : '/^([*+!.&#$¦\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i' ; if (preg_match($regex, trim($email), $matches)) { return array($matches[1], $matches[2]); } else { return false; } }
This function checks whether the user email entered in the text box is valid or not. Call this function on submit.
Javascript Function for validating email
function callEmailCheck() { var emailreg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; //useremail is the name of the email text field. var email = document.getElementById("useremail").value; if(emailreg.test(email) == true){ return email; } return ""; }