Server Side Programming Articles - Page 1832 of 2650

Score After Flipping Matrix in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:22:45

188 Views

Suppose we have a two dimensional matrix A where each value is 0 or 1. Here a move consists of choosing any row or column, and toggling each value in that row or column: changing all 0s to 1s, and all 1s to 0s. Now after making any number of moves, every row of this matrix is interpreted as a binary number, and the score of the matrix is the sum of these numbers. So our task is to find the highest possible score. If the input is like −001110101100The output will be 39 as after toggling, the matrix will ... Read More

How to prevent multiple inserts when submitting a form in PHP?

AmitDiwan
Updated on 09-Apr-2020 13:40:58

2K+ Views

PHP session can be used to prevent multiple inserts when submitting a form. PHP session sets a session variable (say $_SESSION['posttimer']) that sets the current timestamp on POST. Before processing the form in PHP, the $_SESSION['posttimer'] variable is checked for its existence and checked for a specific timestamp difference (say 2 or 3 seconds). This way, those insertions that are actually duplicates can be identified and removed.Simple form −// form.html         The reference to ‘my_session_file.php’ in the above will have the below lines of code −Exampleif (isset($_POST) && !empty($_POST)) {    if (isset($_SESSION['posttimer'])) {     ... Read More

PHP Casting Variable as Object type in foreach Loop

AmitDiwan
Updated on 09-Apr-2020 11:44:45

1K+ Views

This depends on the IDE that is being used. For example, Netbeans and IntelliJ can enable the usage of @var in a comment −/* @var $variable ClassName */ $variable->This way, the IDE would know that the ‘$variable’ is a class of the ClassName after the hint ‘->’ is encountered.In addition, an @return annotation can be created with a method that specifies that the return type will be an array of ClassName objects. This data can be accessed using a foreach loop that fetches the values of the objects −function get_object_type() {    return $this->values; } foreach( $data_object-> values as $object_attribute ... Read More

Sort php multidimensional array by sub-value in PHP

AmitDiwan
Updated on 09-Apr-2020 11:43:24

378 Views

The ‘usort’ function can be used to sort multidimensional arrays in PHP. It sorts an array based on a user-defined criteria −Example Live DemoOutputThis will produce the following output −2 4 63 81An array with 4 elements is declared and this array is passed to the usort function, as well as calling the user-defined ‘my_sort’ function on the elements to make sure that the sorting takes place is ascending order.

In PHP, how can I add an object element to an array?

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

3K+ Views

The code is as follows −Example$object = new stdClass(); $object->name = "My name"; $myArray[] = $object;OutputThis will produce the following output −Suppose myArray already contains ‘a’ and ‘c’, the value of “My name” will be added to it. It becomes Array {    a:0, c:1, “My name”:2 }The object is created and then it is pushed to the end of the array (that was previously present).Alternative$myArray[] = (object) ['name' => 'My name'];

Detect language from string in PHP

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

1K+ Views

The language can’t be detected from the character type. There are other ways, but they don’t guarantee complete accuracy. The ‘TextLanguageDetect Pear Package’ can be used with decent accuracy. Below is a sample code for the same −Examplerequire_once 'Text/LanguageDetect.php'; $l = new Text_LanguageDetect(); $result = $l->detect($text, 4); if (PEAR::isError($result)) {    echo $result->getMessage(); } else {    print_r($result); }OutputThis will produce the following output −Array (    [german] => 0.407037037037    [dutch] => 0.288065843621    [english] => 0.283333333333    [danish] => 0.234526748971 )It is easy to use and has 52 language databases. But the downside is that the Eastern Asian languages can’t be detected using this package.

How to read only 5 last line of the text file in PHP?

AmitDiwan
Updated on 09-Apr-2020 11:39:18

649 Views

To read only 5 last lines of the text file, the code is as follows −Example$file = file("filename.txt"); for ($i = max(0, count($file)-6); $i < count($file); $i++) {    echo $file[$i] . ""; }OutputThis will produce the following output −Given that the file has more than 5 lines of text, the last 5 lines of the text file will be displayed.The file is opened and the number of lines in the file are counted, and beginning from the last line, 5 lines are read.

How to download large files through PHP script?

AmitDiwan
Updated on 09-Apr-2020 11:37:51

3K+ Views

To download large files through PHP script, the code is as follows −ExampleOutputThis will produce the following output −The large file will be downloaded.The function ‘readfile_chunked’ (user defined) takes in two parameters- the name of the file and the default value of ‘true’ for the number of bytes returned meaning that large files have been successfully downloaded. The variable ‘chunksize’ has been declared with the number of bytes per chunk that needs to be read. The ‘buffer’ variable is assigned to null and the ‘cnt’ is set to 0. The file is opened in binary read mode and assigned the ... Read More

Make PHP pathinfo() return the correct filename if the filename is UTF-8

AmitDiwan
Updated on 09-Apr-2020 11:35:56

315 Views

Most of the core PHP functions don’t deal with character sets apart from Latin-1. But before the ‘pathinfo’, placing the ‘setlocale’ can be used to return the correct filename even if it is UTF-8 encoded.By default, it runs with ‘C’ locale, and CLI scripts run with a default utf-8 locale. The locale on the server should be changed from ‘C’ to ‘C.UTF-8’ or to ‘en_US.UTF-8’ before calling the other functions.setlocale(LC_ALL,'en_US.UTF-8'); pathinfo($OriginalName, PATHINFO_FILENAME); pathinfo($OriginalName, PATHINFO_BASENAME);

How do I iterate through DOM elements in PHP?

AmitDiwan
Updated on 09-Apr-2020 11:34:16

1K+ Views

Following is the XML data (input) −                       Iterating through the elements in the DOM object.Example$elements = $dom->getElementsByTagName('foo'); $data = array(); foreach($elements as $node){    foreach($node->childNodes as $child) {       $data[] = array($child->nodeName => $child->nodeValue);    } }OutputThis will produce the following output −Every ‘foo’ tag will be iterated over and specific ‘bar’ and ‘pub’ value will be obtained, i.e specific child nodes can be accessed by their names itself.The elements in the XML file are obtained by ... Read More

Advertisements