Latest Post
Tampilkan postingan dengan label Reading text. Tampilkan semua postingan
Tampilkan postingan dengan label Reading text. Tampilkan semua postingan

Reading Excel Documents from PHP applications

Written By Unknown on Senin, 24 Mei 2010 | 22.29


In this article we will learn on how we can read Microsoft Excel Sheet in PHP. To achieve this we will be using Open Source Tool PHPExcelReader. It provides us with necessary API that allow us to read the Excel Sheet in PHP.


Directory Structure for ExcelReader:
When you will unzip the excelreader, you will find example.php that shows us how to read excel sheet using php code. And the most important thing is the Excel folder, it contains all the necessary files and API that will actually perform your operation. Keep the Excel folder as it is as files are included in the API code as per the directory structure.

Example:
Well the included example.php is sufficient to understand the functionality of the ExcelReader. But i am showing the same code here with slight modification.

< ?php
 
require_once 'Excel/reader.php';
$data = new Spreadsheet_Excel_Reader();
 
// Set output Encoding.
$data->setOutputEncoding('CP1251');
 
/***
* if you want you can change 'iconv' to mb_convert_encoding:
* $data->setUTFEncoder('mb'); *
** /
 /***
* By default rows & cols indeces start with 1
* For change initial index use:
*/
* $data->setRowColOffset(0); *
*
 
/***
* Some function for formatting output.
* $data->setDefaultFormat('%.2f');
columns with unknown formatting * * $data->setColumnFormat(4, '%.3
* setDefaultFormat - set format fo rf'); * setColumnFormat - set format for column (apply only to number fields) *
** /
 $data->read('test.xls'); //Passing the excel sheet to be read from PHP
error_reporting(E_ALL ^ E_NOTICE);
 
for ($i = 0; $i < = $data->sheets[0]['numRows']; $i++) {
echo $data->sheets[0]['cells'][$i][1]."<br />";
}
 
?>
 
 
The most important thing here is:
DescriptionSource Code
Accessing Specific Sheet object inside excel document$data->sheets[0](Accessing 1st sheet)
Accessing total rows inside specific sheet$data->sheets[0]['numRows']
Accessing individual cells data$data->sheets[0]['cells'][0][1] (Here i am accessing 1st sheet cell[0][1] i.e. A1 cell)
If we know this basic code than we can read the data inside the excel sheet from php.

Reading the "clean" text from PDF with PHP

Written By Unknown on Sabtu, 13 Maret 2010 | 00.39


Portable Document Format (PDF) is a file format created for the document exchange. Each PDF file encapsulates a complete description of a fixed-layout 2D document (and, with Acrobat 3D, embedded 3D documents) that includes the text, fonts, images, and 2D vector graphics which compose the documents.

PDF file structure
At first let’s look into the PDF file.

PDF implements documents as a hierarchy of tagged objects organized into trees and/or linked lists. The objects can encapsulate various types of content, or attributes, or pointers to external resources.
There are eight basic kinds of objects in PDF: Booleans, numbers, names, strings, arrays, dictionaries, streams and the null object. Let’s take a look at some of these objects we need to work with.
Strings
In PDF a string consists of a series of 8-bit bytes surrounded by parentheses. A string can be divided into several lines by using the backslash (\) at the end of the line. The backslash itself is not considered as part of the string. For example:
( This is a string. )
( This is a longer \
string. )
Any 8-bit value can be represented either by its octal equivalent (in the form \ddd, where ddd is the octal number), or by its two-digit hex equivalent, surrounded by angle brackets. Later we will search for the text data in the strings.
Arrays
An array is a sequence of PDF objects, enclosed in square brackets. For example:
[(Hello,)10(world!)]
Dictionaries
A dictionary is the key/value pairs, enclosed in two left angle brackets (<<) in the beginning and two right angle brackets (>>) at the end:
<< /Length 4 0 R
   /Filter /FlateDecode
>>
A dictionary is used to assign some properties to an object. We will use these data to determine how to decrypt the stream, find its length, or, for example, omit the current object (if it is an image).
Streams
A stream is a sequence of 8-bit bytes between the keywords stream and endstream. Any type of content made up of raw binary data is represented by a stream.
Streams are represented as objects (see below), which also means the stream will be bracketed by obj andendobj keywords. Before the stream keyword there must be a stream attribute dictionary, giving information about stream length (/Length key) and, often, the kind of compression employed (/Filter key).
As an example, a small text stream might look like:
2 0 obj
<<
/Length 39
>>
stream
BT
/F1 12 Tf
72 712 Td (A short text stream.) Tj
ET
endstream
endobj
In this example, the text itself is given as a string followed by the display text operator Tj.
Objects
An object can enclose the content of any PDF data types (Boolean, number, name, string, etc.), bracketed between obj and endobj keywords. We are primarily interested in objects with the streams inside.
How to get “clean” text?
So, where should we look for text objects in a PDF-document? The answer is simple: we look for objects that contain streams.
Another few things we need to consider:
  • The text in a stream is enclosed between BT (beginning of text) and ET (end of text) keywords.
  • PDF displays a text if there is Tj (display text) or TJ (display text considering the individual character positioning) keyword after a text string or an array of strings.
  • PDF supports the individual character positioning. This means that we can set arbitrary and individual size of the distance between each pair of characters.
  • PDF supports composite fonts where a single character is encoded by one or more bytes of the string. In this case the code lengths and the mappings from codes to glyphs are defined in a data structure called a CMap. PDF also uses a special ToUnicode CMaps to map character codes to Unicode values.
Let’s read!
Now we have obtained enough theoretical knowledge to read our first PDF file. Below you can find the most interesting code parts with comments and the link to the source code.
function pdf2text($filename) {

    // Read the data from pdf file
    $infile = @file_get_contents($filename, FILE_BINARY);
    if (empty($infile))
        return "";

    // Get all text data.
    $transformations = array();
    $texts = array();

    // Get the list of all objects.
    preg_match_all("#obj(.*)endobj#ismU", $infile, $objects);
    $objects = @$objects[1];

    // Select objects with streams.
    for ($i = 0; $i < count($objects); $i++) {
        $currentObject = $objects[$i];

        // Check if an object includes data stream.
        if (preg_match("#stream(.*)endstream#ismU", $currentObject, $stream)) {
            $stream = ltrim($stream[1]);

            // Check object parameters and look for text data.
            $options = getObjectOptions($currentObject);
            if (!(empty($options["Length1"]) && empty($options["Type"]) && empty($options["Subtype"])))
                continue;

            // So, we have text data. Decode it.
            $data = getDecodedStream($stream, $options); 
            if (strlen($data)) {
                if (preg_match_all("#BT(.*)ET#ismU", $data, $textContainers)) {
                    $textContainers = @$textContainers[1];
                    getDirtyTexts($texts, $textContainers);
                } else
                    getCharTransformations($transformations, $data);
            }
        }

    }

    // Analyze text blocks taking into account character transformations and return results.
    return getTextUsingTransformations($texts, $transformations);
}
You can find the source code HERE.
We must say that this code will parse correctly the simple PDF files. You can use this code as a basis and improve it according to your needs.
Usage example
To read PDF file (e.g. sample.pdf) and display received plain text in the browser window, add the following code to the source code before the first function.
$result = pdf2text ('sample.pdf');
echo $result;
Do not forget to replace sample.pdf with your PDF file name.
Related information


PHP: Reading the "clean" text from RTF


Rich Text Format (often abbreviated as RTF), to surprise of many, is quite complex text data format. During its long history RTF bought a lot of add-ons that disturb the process of getting "clean" text. Let's try to solve that...

A little theory
At first let’s look into the RTF file

An RTF file consists of unformatted text, control words and groups.
control word is a specially formatted command that RTF uses to mark printer control codes and information that applications use to manage documents. A control word is made up of lowercase alphabetic characters between "a" and "z". Each control word begins with a backslash (\) and ends with one of the following:
  • A space. In this case, the space is part of the control word.
  • A numeric parameter that can be a positive or a negative number.
  • Any character other than a letter or a digit.
Therefore, the character string \rtf1\ansi\ansicpg1251 easily can be divided into three control words: rtf with parameter 1 (the major format version), ansi (the current encoding) and ansicpg with parameter 1251 (the current code page number 1251).
group consists of text and control words enclosed in braces ({}). Control words defined within a group affect only the text inside this group and all nested subgroups. In order to know which control words are active now we will use the control words stack. When reading the opening brace ({) we will add new array stack element and write the data from previous stack element to it. When reading the closing brace (}) – remove top stack element.
Also we need to mention that some control words may be turned off not by closing the group but adding parameter 0 to the control word. For example, strings This is {\b bold} text and This is \b bold \b0 text give us the same result This is bold text.
Now we can come to the conclusion that all characters in an RTF file that are not control words or braces are plain text.
How to get "clean" text?
Even if we already know how to distinguish plain text from the control words, we need to discuss the characters encoding question.
RTF is an 8-bit format. That would limit it to ASCII, but RTF can encode characters beyond ASCII by escape sequences. The character escapes are of two types: code page escapes and Unicode escapes.
In a code page escape, two hexadecimal digits following an apostrophe (\'hh) are used for denoting a character taken from a Windows code page. The current code page is specified by control word \ansicpg. For example, if/ansicpg1256 is present, the sequence \'c8 will encode the Arabic letter beh (ุจ).
If a Unicode escape is required, the control word \u is used, followed by a 16-bit signed decimal integer giving the Unicode codepoint number. For the benefit of programs without Unicode support, this must be followed by the nearest representation of this character in the specified code page. For example, \u1576? would give the Arabic letter beh, specifying that older programs which do not have Unicode support should render it as a question mark instead.
The control word \uc0 can be used to indicate that subsequent Unicode escape sequences within the current group do not specify a substitution character.
Let’s read!
Now we have enough theoretical knowledge to start reading our first .rtf files:
<?php

// Function that checks whether the data are the on-screen text.
// It works in the following way:
// an array arrfailAt stores the control words for the current state of the stack, which show that
// input data are something else than plain text.
// For example, there may be a description of font or color palette etc. 
function rtf_isPlainText($s) {
    $arrfailAt = array("*", "fonttbl", "colortbl", "datastore", "themedata");
    for ($i = 0; $i < count($arrfailAt); $i++)
        if (!empty($s[$arrfailAt[$i]])) return false;
    return true;

function rtf2text($filename) {
    // Read the data from the input file.
    $text = file_get_contents($filename);
    if (!strlen($text))
        return "";

    // Create empty stack array.
    $document = "";
    $stack = array();
    $j = -1;
    // Read the data character-by- character…
    for ($i = 0, $len = strlen($text); $i < $len; $i++) {
        $c = $text[$i];

        // Depending on current character select the further actions.
        switch ($c) {
            // the most important key word backslash
            case "\\":
                // read next character
                $nc = $text[$i + 1];

                // If it is another backslash or nonbreaking space or hyphen,
                // then the character is plain text and add it to the output stream.
                if ($nc == '\\' && rtf_isPlainText($stack[$j])) $document .= '\\';
                elseif ($nc == '~' && rtf_isPlainText($stack[$j])) $document .= ' ';
                elseif ($nc == '_' && rtf_isPlainText($stack[$j])) $document .= '-';
                // If it is an asterisk mark, add it to the stack.
                elseif ($nc == '*') $stack[$j]["*"] = true;
                // If it is a single quote, read next two characters that are the hexadecimal notation
                // of a character we should add to the output stream.
                elseif ($nc == "'") {
                    $hex = substr($text, $i + 2, 2);
                    if (rtf_isPlainText($stack[$j]))
                        $document .= html_entity_decode("&#".hexdec($hex).";");
                    //Shift the pointer.
                    $i += 2;
                // Since, we’ve found the alphabetic character, the next characters are control word
                // and, possibly, some digit parameter.
                } elseif ($nc >= 'a' && $nc <= 'z' || $nc >= 'A' && $nc <= 'Z') {
                    $word = "";
                    $param = null;

                    // Start reading characters after the backslash.
                    for ($k = $i + 1, $m = 0; $k < strlen($text); $k++, $m++) {
                        $nc = $text[$k];
                        // If the current character is a letter and there were no digits before it,
                        // then we’re still reading the control word. If there were digits, we should stop
                        // since we reach the end of the control word.
                        if ($nc >= 'a' && $nc <= 'z' || $nc >= 'A' && $nc <= 'Z') {
                            if (empty($param))
                                $word .= $nc;
                            else
                                break;
                        // If it is a digit, store the parameter.
                        } elseif ($nc >= '0' && $nc <= '9')
                            $param .= $nc;
                        // Since minus sign may occur only before a digit parameter, check whether
                        // $param is empty. Otherwise, we reach the end of the control word.
                        elseif ($nc == '-') {
                            if (empty($param))
                                $param .= $nc;
                            else
                                break;
                        } else
                            break;
                    }
                    // Shift the pointer on the number of read characters.
                    $i += $m - 1;

                    // Start analyzing what we’ve read. We are interested mostly in control words.
                    $toText = "";
                    switch (strtolower($word)) {
                        // If the control word is "u", then its parameter is the decimal notation of the
                        // Unicode character that should be added to the output stream.
                        // We need to check whether the stack contains \ucN control word. If it does,
                        // we should remove the N characters from the output stream.
                        case "u":
                            $toText .= html_entity_decode("&#x".dechex($param).";");
                            $ucDelta = @$stack[$j]["uc"];
                            if ($ucDelta > 0)
                                $i += $ucDelta;
                        break;
                        // Select line feeds, spaces and tabs.
                        case "par": case "page": case "column": case "line": case "lbr":
                            $toText .= "\n"; 
                        break;
                        case "emspace": case "enspace": case "qmspace":
                            $toText .= " "; 
                        break;
                        case "tab": $toText .= "\t"; break;
                        // Add current date and time instead of corresponding labels.
                        case "chdate": $toText .= date("m.d.Y"); break;
                        case "chdpl": $toText .= date("l, j F Y"); break;
                        case "chdpa": $toText .= date("D, j M Y"); break;
                        case "chtime": $toText .= date("H:i:s"); break;
                        // Replace some reserved characters to their html analogs.
                        case "emdash": $toText .= html_entity_decode("&mdash;"); break;
                        case "endash": $toText .= html_entity_decode("&ndash;"); break;
                        case "bullet": $toText .= html_entity_decode("&#149;"); break;
                        case "lquote": $toText .= html_entity_decode("&lsquo;"); break;
                        case "rquote": $toText .= html_entity_decode("&rsquo;"); break;
                        case "ldblquote": $toText .= html_entity_decode("&laquo;"); break;
                        case "rdblquote": $toText .= html_entity_decode("&raquo;"); break;
                        // Add all other to the control words stack. If a control word
                        // does not include parameters, set &param to true.
                        default:
                            $stack[$j][strtolower($word)] = empty($param) ? true : $param;
                        break;
                    }
                    // Add data to the output stream if required.
                    if (rtf_isPlainText($stack[$j]))
                        $document .= $toText;
                }

                $i++;
            break;
            // If we read the opening brace {, then new subgroup starts and we add
            // new array stack element and write the data from previous stack element to it.
            case "{":
                array_push($stack, $stack[$j++]);
            break;
            // If we read the closing brace }, then we reach the end of subgroup and should remove 
            // the last stack element.
            case "}":
                array_pop($stack);
                $j--;
            break;
            // Skip “trash”.
            case '\0': case '\r': case '\f': case '\n': break;
            // Add other data to the output stream if required.
            default:
                if (rtf_isPlainText($stack[$j]))
                    $document .= $c;
            break;
        }
    }
    // Return result.
    return $document;
}
?>
Conclusion
This code will cope correctly with the majority of .rtf files. However, there are several ways to improve it. First, you can add the additional checks for non-textual data. Given code will cut off only the fonts, colors, theme design, binary data and everything that is marked as "do not read me if you cannot" (text marked with \ *). Second, you may add the further encoding and code page parsing to reflect the \ 'hh keywords more accurately.
 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Kumpulan Kata Broadcast Blackberry - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger