Found 1046 Articles for PHP

How can I extract or uncompress gzip file using php?

AmitDiwan
Updated on 09-Apr-2020 11:15:49

888 Views

The zipped files can be unzipped or decompressed using PHP’s gzread function. Below is the code example for the same −Example$file_name = name_of/.dump.gz'; $buffer_size = 4096; // The number of bytes that needs to be read at a specific time, 4KB here $out_file_name = str_replace('.gz', '', $file_name); $file = gzopen($file_name, 'rb'); //Opening the file in binary mode $out_file = fopen($out_file_name, 'wb'); // Keep repeating until the end of the input file while (!gzeof($file)) {    fwrite($out_file, gzread($file, $buffer_size)); //Read buffer-size bytes. } fclose($out_file); //Close the files once they are done with gzclose($file);OutputThis will produce the following output −The uncompressed data ... Read More

Why is PHP apt for high-traffic websites?

AmitDiwan
Updated on 09-Apr-2020 11:14:23

205 Views

The main reason behind websites being slow is overloading of hosts.The benefit of using PHP over other compiled languages is the ease of maintenance. PHP has been designed ground up to efficiently handle HTTP traffic, there is less to build in comparison to building using other compiled languages. In addition to this, merging changes is an easier task since the server doesn’t need to be recompiled and restarted (which needs to be done in case of using a compiled binary- FastCGI).PHP, when properly written, can be scaled to a great extent. Other limiting factors include the database engine that is ... Read More

Is there a PHP function that only adds slashes to double quotes NOT single quotes

AmitDiwan
Updated on 09-Apr-2020 11:13:13

486 Views

The json_encode function can be used to add slashes to double quotes. Also ‘addcslahses’ can also be used to add ‘\’ to specific characters −Example Live DemoOutputThis will produce the following output −Hello \there!The ‘addcslashes’ function is used to return a string with backslashes in front of the specific characters. It is a case sensitive function and should usually not be used with 0 (null), r (carriage return), n (newline), f (form read), t (tab), v (vertical tab) values. This is because values like \0, \r, , \t, \f and \v are pre-define escape sequences.In the above code, the ‘addcslashes’ function ... Read More

How to redirect domain according to country IP address in PHP?

AmitDiwan
Updated on 09-Apr-2020 11:12:10

1K+ Views

The GeoIP extension can be used to find the exact location of an IP address. Apart from this, the geoPlugin class can be downloaded from −http://www.geoplugin.com/_media/webservices/geoplugin.class.phpsThe country code list can be found in the below link −http://www.geoplugin.com/iso3166An index.php file can be placed inside the root folder and the below lines of code can be put inside this index file −Once the geoplugin class has been downloaded, a new instance is created and given the name ‘geoplugin’. The locate function is called on this instance of the geoplugin class. The same class object’s countryCode is assigned to a variable named ‘var_country_code’. ... Read More

PHP: Remove object from array

AmitDiwan
Updated on 09-Apr-2020 11:10:50

4K+ Views

The unset function can be used to remove array object from a specific index in PHP −Example Live Demo$index = 2; $objectarray = array(    0 => array('label' => 'abc', 'value' => 'n23'),    1 => array('label' => 'def', 'value' => '2n13'),    2 => array('label' => 'abcdef', 'value' => 'n214'),    3 => array('label' => 'defabc', 'value' => '03n2') ); var_dump($objectarray); foreach ($objectarray as $key => $object) {    if ($key == $index) {       unset($objectarray[$index]);    } } var_dump($objectarray);OutputThis will produce the following output −array(4) { [0]=> array(2) { ["label"]=> string(3) "abc" ["value"]=> string(3) "n23" } [1]=> ... Read More

PHP: How do I display the contents of a textfile on my page?

AmitDiwan
Updated on 09-Apr-2020 11:08:04

2K+ Views

The file_get_contents function takes the name of the php file and reads the contents of the text file and displays it on the console. get the contents, and echo it out.The contents of filename.php would be the output.In the above code, the function ‘file_get_contents’ is called by passing the php file name. The output will be the contents present in the php file.

How to display XML in HTML in PHP?

AmitDiwan
Updated on 09-Apr-2020 11:06:47

1K+ Views

If the string is pre-formatted, and a plain text representation of the same is needed, it can be wrapped in a HTML tag and html entities can be used to escape the angle brackets. This has been shown below −The string is assigned to a string type and the above is used to show XML in HTML −Example Live DemoOutputThis will produce the following output −           EXAMPLE    

Remove duplicated elements of associative array in PHP

AmitDiwan
Updated on 09-Apr-2020 11:04:40

2K+ Views

The ‘array_map’ function sends the value of every element in the array to a user-defined function. It then returns an array with new values, because of calling the user-defined function on the array.Syntax of array_map functionarray_map ( user-defined function, array_1, array_2, array_3…)The user-defined function and the array_1 are compulsory arguments, but array_2 and array_3 are optional.Example Live Demo$result = array(    0=>array('a'=>1, 'b'=>'Hello'),    1=>array('a'=>1, 'b'=>'duplicate_val'),    2=>array('a'=>1, 'b'=>'duplicate_val') ); $unique = array_map("unserialize", array_unique(array_map("serialize", $result))); print_r($unique);OutputThis will produce the following output −Array ( [0] => Array ( [a] => 1 [b] => Hello ) [1] => Array ( [a] => 1 ... Read More

Read last line from file in PHP

AmitDiwan
Updated on 09-Apr-2020 11:03:02

2K+ Views

To read last line from file in PHP, the code is as follows −$line = ''; $f = fopen('data.txt', 'r'); $cursor = -1; fseek($f, $cursor, SEEK_END); $char = fgetc($f); //Trim trailing newline characters in the file while ($char === "" || $char === "\r") {    fseek($f, $cursor--, SEEK_END);    $char = fgetc($f); } //Read until the next line of the file begins or the first newline char while ($char !== false && $char !== "" && $char !== "\r") {    //Prepend the new character    $line = $char . $line;    fseek($f, $cursor--, SEEK_END);    $char = fgetc($f); ... Read More

Multiple index variables in PHP foreach loop

AmitDiwan
Updated on 09-Apr-2020 11:01:08

4K+ Views

The ‘foreach’ loop can be used to multiple index variables of two arrays. This has been shown below −Example Live DemoOutputThis will produce the following output −aB12 PQ34 cd90 pm49Note − If there are more than 2 arrays, nested ‘foreach’ loops can be used.Here, 2 arrays have been declared and they are being traversed using the ‘foreach’ loop. The result is that the respective index of every array is matched and the data at those indices are displayed one next to the other.

Advertisements