Found 1060 Articles for PHP

How to display XML in HTML in PHP?

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

2K+ 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

3K+ 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

5K+ 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.

Strip punctuation with PHP

AmitDiwan
Updated on 09-Apr-2020 10:59:31

1K+ Views

The ‘preg_replace’ function can be used to match the characters in the string and remove the unnecessary characters.To keep letters and numbers −Example Live DemoOutputThis will produce the following output −Hello my name is Bobby I am 8 yearsTo keep letters only −Example Live DemoOutputThis will produce the following output −Hello my name is Bobby I am yearsTo keep letters, numbers and underscoreExampleOutputThis will produce the following output −Hello my name is Bobby I am 8 years

Detect base64 encoding in PHP?

AmitDiwan
Updated on 09-Apr-2020 10:49:18

677 Views

To detect base64 encoding in PHP, the code is as follows −Example Live Demo

How can I remove all empty values when I explode a string using PHP?

AmitDiwan
Updated on 09-Apr-2020 10:48:09

1K+ Views

The array_filter() or PREG_SPLIT_NO_EMPTY option on preg_split() can be used to remove empty values from a string when it is exploded −Example Live Demo

PHP: fopen to create folders

AmitDiwan
Updated on 09-Apr-2020 10:44:09

2K+ Views

The fopen can’t be used to create directories. This is because fopen function doesn't create or open folders, it only works with files.Before using the fopen function, one should check with is_dir first if it exists, if not create it using the mkdir function −$filename = '/path/to /file.txt'; $dirname = dirname($filename); if (!is_dir($dirname)) {    mkdir($dirname, 0755, true); }The above code creates a path to the file named ‘filename’. The directory of the ‘filename’ is obtained using the ‘dirname’ function. Next, this directory is checked for the existence using the ‘is_dir’ function. If the directory already exists, no operation takes ... Read More

Check if a PHP cookie exists and if not set its value

AmitDiwan
Updated on 09-Apr-2020 13:51:39

2K+ Views

Based on the PHP manual, the existence of a cookie can’t be found.A reference from the manual: “Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.”The reason being cookies are response headers to the browser and the browser needs to then send them back along with the next request. This is the reason they are available on the second page load only.But here is a work around for the same: The $_COOKIE can be set when the setcookie function is called −if(!isset($_COOKIE['lg'])) {    setcookie('lg', 'ro');    $_COOKIE['lg'] ... Read More

How to import csv file in PHP?

AmitDiwan
Updated on 09-Apr-2020 10:40:30

1K+ Views

The below code can be used to import CSV file in PHP −The file will display the contents of the csv file are displayed on the screen.In the above code, beginning from row 1 (since row 0 would usually contain headers/column names), the csv file is opened in read mode and the fgetcsv function is called to read in 1000 rows of data present in the csv file.The number of columns in every row is displayed along with the contents of it.

Advertisements