PhpFunction array uniqueRemove duplicate values in a arrayThe below script helps you to remove duplicate values in a array <?php $remove_duplicate=array('calc',"math","calc"); print_r($remove_duplicate); echo "Script to remove duplicate values"; print_r(array_unique($remove_duplicate)); // array_unique! ?> OUTPUT: Array ( [0] => calc [1] => math [2] => calc ) Script to remove duplicate values Array ( [0] => calc [1] => math ) In the above script the calc word which .. How to find file and directoryScript to list directory content<?php /* function that reads directory content and returns the result as links to every file in the directory also it display type whether its a file or directory */ function DirDisply() { $TrackDir=opendir("."); echo ""; while ($file = readdir($TrackDir)) { if ($file == "." || $file == "..") { } else { .. GLOB_ONLYDIR functionList all the base directories in linux os<?php $directories = glob($somePath . '/*' , GLOB_ONLYDIR); print_r($directories); ?> OUTPUT: Array ( [0] => /bin [1] => /boot [2] => /cgroup [3] => /dev [4] => /etc .... ) The above is a sample output. The above code is used to print the directories using php code. .. Script to list directory and its filesList folder sub folder and its filefunction ListFiles($dir) { if($dh = opendir($dir)) { $files = Array(); $inner_files = Array(); while($file = readdir($dh)) { if($file != "." && $file != ".." && $file[0] != '.') { if(is_dir($dir . "/" . $file)) { $inner_files = ListFiles($dir . "/" . $file); if(is_array($inner_files)) $files = array_merge($files, $inner_files); } else { array_push($files, $dir . "/" . $file); } } } closedir($dh); return $files; } } foreach (ListFiles('../modules') as .. Backup mysql database scriptAutomate export and import mysql database<?php error_reporting(E_ALL & ~E_NOTICE); $hostname="localhost"; $username="root"; $password="mypass"; $dbname="backupdb"; backup_tables($hosthost,$username,$password,$dbname); function backup_tables($host,$user,$pass,$name,$tables = '*') { $link = mysql_connect($host,$user,$pass); mysql_select_db($name,$link); //get all of the tables if($tables == '*') { $tables = array(); $result = mysql_query('SHOW TABLES'); while($row = mysql_fetch_row($result)) { $tables[] = $row[0]; } } else { $tables = is_array($tables) ? $tables : explode(',',$tables); } foreach($tables as $table) { $result = mysql_query('SELECT * FROM '.$table); $n_fields = mysql_num_fields($result); $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table)); $return.= "\n\n".$row2[1].";\n\n"; for ($i = 0; $i < $n_fields; .. How to get ip address of a domainFetch IP-Address of a websiteTo get an IP of a website in php, generally we use, gethostbyname("example.com"); This will work nicely, but for some urls instead of returning IP of the given url this function outputs url what you typed. For example, we can try a url which is redirected to another url then .. How to use ctype functionValidate numeric or sting valueHow to validate the given string using ctype function? Normally I used to use preg function to validate the string. Some days ago I rad in php manual that the preg function is slower than other validating function, but still has its own pons and cons. Today I came across a .. Progress bar scriptPhp progress in terminal using phpThe below script will help you see progress bar on your terminal. When we are doing some big task and it seems that the processing time will take some time in the background then we can use the progress bar on foreground. The below code will show progress status from .. How to get page name using basenameParse url and get the page nameHow to get the page name from a url? To parse the url of the page you present in the sub-directory, Earlier I use to use the parse_url function to get current page. Today I experience a different method to extract the page name using another different function using basename. If .. Error php file get contents no imagesFile_get_contents does not display imagesSome times file_get_contents($url) will not display images in the $url. This below code rectifies the issue. <?php $URL = "http://www.indiandir.com/addurl.php"; $domain = file_get_contents($URL); if($domain !== FALSE) { $domain = preg_replace('/<head>/i', "<head>\n<base href='$URL'>\n", $domain); echo $domain; } ?> .. Code to identify the mobile deviceMobile Redirection using php codeHow to identify the mobile device using php code? The mobile device can be identified by using HTTP_USER_AGENT function, once the device is identified then it can be redirected to a different page which is compatible to mobile device. mobile = "http://mobile.MY_DOMAIN.com"; $text = $_SERVER['HTTP_USER_AGENT']; $var[0] = 'Mozilla/4.'; $var[1] = 'Mozilla/3.0'; $var[2] = 'AvantGo'; $var[3] = 'ProxiNet'; $var[4] .. Find memory usage of php scriptCalculate memory usage of php scriptAt times you might have a requirement to find how much memory is consumed by your PHP script when the script is executed. You might require this information if your script has a memory leak or takes a long time to execute. If that's the case then you can use .. Simple if else code reduce coding linesIf else statementIn general we write if else as if($a) { print $b; } else { print $c; } Reduce the if else code by ? : print ( $a ? $b : $c ); .. Trim white space in phpTrim functionStrip or trim whitespace in php using trim(). Remove unwanted space from your code. <?php $hello = " Hello w3calculator.com "; $trimm = trim($hello); echo $trimm; ?> Output: Hello w3calculator.com In the above output you can see the white space were removed. .. Get total number of rows from mysql tableCount the rows or data in mysql tableThe below code will help you to get the total number of rows from the mysql table. It will just print the number of rows of a table. <?php $link = mysql_connect("localhost", "mysql_user", "mysql_pass"); mysql_select_db("database", $link); $result = mysql_query("SELECT * FROM table1", $link); $num_rows = mysql_num_rows($result); echo "$num_rows Rows\n"; ?> .. |