chapter6PHPTUT-nicephotog

download chapter6PHPTUT-nicephotog

of 2

Transcript of chapter6PHPTUT-nicephotog

  • 8/9/2019 chapter6PHPTUT-nicephotog

    1/2

    Chapter 7 PHP Objects (Code operation optimising and Classes)

    PHP File I/O and String Regular Expression engine

    Types of files

    Before doing anything further it is wise to explain a small problem that is a difference between file handling in Windows/DOS systemsand UNIX systems e.g. Linx. Because two data handling methods of file read/write interaction occur.Windows/DOS requires you to rememberto open binary files or any file not read/written by text read/write methods using the "b" setting. In PERL the file handle for the open() functionis declared as "(binmode)" and as such "b" is the representation to the fopen() by setting in PHP to do a binary read.You must use "b" as you

    also must in PERL use "(binmode)" or damage to the server OS will result in windows/DOS. Often this is explained as opening binary files butthat is irrelevant as i said before text and data are bytes and the server does not have much of a clue except file extensions on their file names,and only uses them for its server application specified purpose.The purpose of programming is to customise and commit efficiency by customisedapplication interaction and operation. It is only relevant the read/write method you use not the files type/MIME type at any time.While using a unix server it does not bother it whether the read is binary or text it is decided as good practice to always use "b" setting in all scriptsas applicable to the read/write method not the files data composition.So there are binary and text files, well actually there are more.Other files are closer to addresses and are referred to in web programming andnetworking as resources or URL's(universal resource Location) or URI's(universal resource Identity).Here we are more interested with the URIIdentity because another type of file is for networking and called a socket(for other purposes a port). As confusing as this previous another typeof file(an associative pair of them) also of importance is part of the actual server hardware but quite different to a socket but not particularlydifferent and are called Standard Input and Standard Output. The main difference is that Standard Input/Output is for the internal interaction ofdata transfer of data and commands with the CPU, and the application processes sending the information.Sockets and ports are for extension ofthe filing system into other machines, an activity called , networking (e.g. routing and bridging) and also extension to external devices e.g. a printer.

    Testing, Opening and closing files in PHP

    Because PHP is heavily constructed for web servers (not that it doesn't contain many other script language uses (including some lower levellanguage functions)) the method of opening and read/writing a file shown here will be conservative of its security requirements by coding. Oftenyou will have trouble setting the policy for the file to be read or created on a web server because of the settings of the PHP security levels.The extra problem comes in that unlike both the PHP manuals example of opening a file using PHP or using a script for e.g. administrative or

    personal use internally in a machine ,opening and reading for security reasons requires two to three tests to be performed before the completeread or write action can be done.First the file to be opened must be tested for its existence.Then it must be tested for its ability to be read/written.This latter is not the same astesting whether or not the read/write occurred.One important point about file handling must be remembered, and that is the mode of fopen() that is set in the second argument to the function.In the script i have set it to "r+" because it allows reading and writing but does not truncate(delete file contents) the file when opened.The warninghere is to always be sure of the setting given to fopen() because some of the settings can truncate the file. The example below is actually moresuitable for binary files, to operate on text it is recommended to use file() or file_get_contents() functions for reading text.

    Regular Expressions (specialised matching of text sequences accurately)

    PHP regular expressions are not much different in syntax from PERL regular expressions at the level of the Patternused inside the template entry. As i wrote before, PHP does much of its operations based on pre defined functioncalls not in semantic syntax.PHP has its own minor difference to PERL in the templates and the PERL templates useabledo not support a limmited set of PERL's problem expression handler pragmas for regular expressions. To start i will gothrough the common set of symbols used in most script engines for constructing regular expression templates. Rememberthese are not the same as the language operators but similar or quite activity specific.! negation (reversal/inverse of current operation value) similar to boolean is not.* find 0 or as many much as wanted? greed operator (find in as short a set as possible)+ find once or more. any character except new line or space (dependent on language and expression engine configuration)() grouping operator (much as in maths)[] class operator (makes a set of characters to find p/single input character e.g. [aCdeFk] )- range operator (from logically ordered set from-to e.g. a group of classes of ([0-9][a-z][A-Z]) )| pipe operator to use in a group (meaning both but either new line or space singularly in the group (\s|\n) )^ start of input of searched string$ end of input of searched string\ escape character

    {} expression find quantity e.g. {2} find twice {4,9} find 4 at least 9 at most.\w a word character\d a digit\W a non word character\D a non digit characterTwo more things to know about PHP regular expressions. The return results are placed in an array of your naming andthe template is fed as a string to the regular expression function not as a bareword symbol set.The escape character is important in regular expressions and use extremely often to build any regular expressiontemplate.Effectively to place any of the above characters into a string to be searched for requires escaping thecharacter inside the string. e.g. to match "$" sign as a searched string requires using "\$" as the template.Thankfully since PHP 4.3.3 \Q \E set of literalisers are available to put in the search string. \Q is the start of theliteralisation and \E ends the literalisation in the template.This set has been available in PERL regular expressionsyntax for some time.In PHP it is a little tricky to use. e.g. to make the template for a variable containginge.g the dollars sign again.

  • 8/9/2019 chapter6PHPTUT-nicephotog

    2/2

    if

    (eregi

    ($template_php,$input)){print('$ '."matched

    "

    );

    }else{print('$ '."did not match

    ");

    }

    if

    (eregi

    ("\$",

    $input)){# exceptprint('$ '."matched

    ");

    }else{print('$ '."did not match

    ");

    }

    $template_php="$";$template_php=quotemeta($template_php);if

    (eregi

    ($template_php,$input)){# eregi() is the non case sensitive function for match testingprint('$ '."matched

    ");

    }else{print('$ '."did not match

    ");

    }

    ?>

    preg_match() and preg_match_all() relate to first singular find and global(all around) find through a searched string.Above php uses a string conditioning function to escape all meta characters passed to the template in a string.Other string operation functions exist of varied levels of accuracy and specialised job but the main functions are tooperate on character case(upper/lower), ignore case through all, replace substring by regular expression , or test formatches.

    preg_match_all() used in the next script example contains four arguments to its call. The last is a changeable flag toknow what to aquire and how to arrange (PREG_PATTERN_ORDER) the results into the returned multi-dimensional array.One of the tools PHP has is the return on the second array of any parenthesised subpatterns.In the script below is the use of the range operator in the square brackets for the template and also use of the pipeoperator and grouping for the an either choice.