AmitDiwan has Published 10744 Articles

Detect language from string in PHP

AmitDiwan

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)) { ... Read More

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

AmitDiwan

AmitDiwan

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

632 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, ... Read More

How to download large files through PHP script?

AmitDiwan

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 ... Read More

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

AmitDiwan

AmitDiwan

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

307 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. ... Read More

How do I iterate through DOM elements in PHP?

AmitDiwan

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) {     ... Read More

What does the '@' prefix do in PHP?

AmitDiwan

AmitDiwan

Updated on 09-Apr-2020 11:31:53

741 Views

The ‘@’ symbol suppresses errors from getting displayed on the screen.PHP supports an error control operator, i.e the sign (@). When it is prepended to an expression in PHP, error messages that could get generated when it uses that expression will be ignored.If the track_errors attribute is enabled, the error ... Read More

Is it possible to get list of defined namespaces in PHP

AmitDiwan

AmitDiwan

Updated on 09-Apr-2020 11:31:04

647 Views

Given file 1 has namespace ns_1 and file 2 has namespace ns_2, if file 1 and file 2 are included in file 3, there is no way to know that namespaces ns_1 and ns_2 have been loaded.The only way is to use the ‘class_exists’ function and the list of classes ... Read More

How to open an Excel file with PHPExcel for both reading and writing?

AmitDiwan

AmitDiwan

Updated on 09-Apr-2020 11:28:33

3K+ Views

There is no concept of opening a file for read and write in PHPExcel since it is not aware of the source of the PHPExcel object. Irrespective of the source from where the file was loaded or the type of file, the file can be read based on its named ... Read More

PHP readfile vs. file_get_contents

AmitDiwan

AmitDiwan

Updated on 09-Apr-2020 11:26:26

1K+ Views

The ‘readfile’ function is an inbuilt function in PHP that reads the file directly into the output buffer. The name of the file is passed as a parameter to the function. It returns the number of bytes that were read in case of successfully reading the data. It returns FALSE ... Read More

Storing objects in PHP session

AmitDiwan

AmitDiwan

Updated on 09-Apr-2020 11:25:00

5K+ Views

The serialize() function in PHP can be used before storing the object, and the unserialize() function can be called when the object needs to be retrieved from the session.The function converts a storable representation of a specific value into a sequence of bits. This is done so that the data ... Read More

Advertisements