Advanced Commercial Web Site Design: ServerSide Scripting - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Advanced Commercial Web Site Design: ServerSide Scripting

Description:

Advanced Commercial Web Site Design: ServerSide Scripting – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 20
Provided by: Gan58
Category:

less

Transcript and Presenter's Notes

Title: Advanced Commercial Web Site Design: ServerSide Scripting


1
Advanced Commercial Web Site Design Server-Side
Scripting
  • Session 7 String and File functions

2
Outline
  • Manipulating Strings
  • Getting specific characters
  • String functions
  • String length and locating specific characters
  • Substrings
  • Parsing strings
  • Tokenizer
  • File handling
  • Open a file
  • Read / Write a file

3
Strings
  • A sequence of characters that are grouped
    together as a unit and
  • can be passed as arguments in methods
  • can be assigned to variables
  • can be outputted to the web-page
  • A string consists of the characters and is
    included in either
  • Single quotes ( ) or hello
  • Double quotes ( ) hello
  • Certain characters cannot by used as they are,
    thus we need to escape them.

4
Characters in Strings
  • A string can be treated as an array of
    characters. The index of the first character is
    0, the second characters index is 1, etc.
  • Example
  • lastName Doe // string
  • // to obtain the first character get the first
    element of the array
  • char1 lastName0 D
  • char1 lastName1 o
  • char3 lastName2 e
  • echo( char3.char1.char2)
  • eDo

5
Length of a String
  • Method strlen(string). The length of the
    specified string is the number of characters this
    string consists of.
  • Example

s1 Hello s2 // to obtain the length
of this string len1 strlen(s1) 5 len2
strlen(s2) 0 echo( Length of s1 len1,
Length of s2 len2) Length of Hello 5,
Length of 0
6
Locate a specific char in a String
  • Method strpos(string, char). Returns the index
    (numeric position) of the specified character of
    its first occurrence in the specified string.
  • Example
  • If the specified character in the specified
    string is not found, the function will return
    FALSE.
  • s1 Hello
  • // to locate a specific character on this string
  • pos1 strpos(s1, e) Hello e
  • pos2 strpos(s1, o) Hello o
  • echo( Index of e in s1 pos1, Index of o in
    s1 pos2)
  • Index of e in Hello 1, Index of o in Hello
    4

7
Substrings of a String
  • Method substr(string, startIndex ,length).
    Returns the portion of the specified string, that
    starts at startIndex until the end. If length is
    specified, it will read length characters
    starting from startIndex.
  • Example
  • If start is negative, the returned string will
    start at the start'th character from the end of
    string.

s1 Hello world! // to obtain a portion of
the original string sub1 substr(s1,
6) sub2 substr(s1, 6, 5) echo(
Substring of s1 starting at 7th character
sub1) Substring of Hello world! starting at
6th character world! echo( Substring of s1
starting at 7th character for 5 chars sub2)
Substring of Hello world! starting at 6th
character for 5 chars world
sub3 substr(s1, -3) ld!
8
Parsing strings
  • The process of breaking up a string into
    substrings is called tokenizing. Special
    delimiters exist between the substrings, and
    method (tokenizer) for tokenizing can pick up
    automatically the delimiters and substrings,
    providing for efficient parsing of the long
    string.
  • Method strtok(str, delimiter)splits a string
    (str) into smaller strings (tokens), with each
    token being delimited by any character from
    token. i.e. strtok breaks a string using single
    _characters_ , the best part is you can use
    multiple characters at the same time.

9
A simple tokenizer
  • Example
  • s1 It is a nice_day!
  • // to obtain tokens, separated by the (space or
    underscore) delimiter
  • token strtok(s1, _)
  • echo token . ltbrgt
  • echo strtok( _) .ltbrgt
  • echo strtok( _) .ltbrgt
  • echo strtok( _) .ltbrgt
  • echo strtok( _) .ltbrgt

10
Another tokenizer
  • Example
  • s1 LastName._.FirstName._.CardNumber
  • // to obtain tokens, separated by the
    (underscore) delimiter
  • tokens explode (_, s1)
  • for(i 0 i lt count(tokens) i)
  • echo tokens i .ltbrgt

Method explode ( string separator, string string
, int limit ) Returns an array of strings,
each of which is a substring of string formed by
splitting it on boundaries formed by the string
separator. If limit is set, the returned array
will contain a maximum of limit elements with the
last element containing the rest of string.
11
Exercise 7.1 Tokenizer
  • Create a long string with a distinctive delimiter
    in between the portions of the string you are
    interested in.e.g. Name Surname Age Height
  • Tokenize the string and output each of the tokens
    on a separate line in the resulting web-page.
    Also output the original string and the delimiter
    used to parse it.
  • Test for correct results for different delimiters!

http//uk2.php.net/strings for detailed
description of String functions
12
File handling
  • Two main operations are defined for (any type of)
    files
  • Read from a file Read the data stored in a file
    and put them in the memory
  • Write to a file Put the data of the memory into
    a file, for later use (persistency)
  • and an optional combined operation
  • Append to a file Write data at the end of an
    existing file (do not overwrite the original file)

13
File handling (cont.)
  • The process of accessing a file for either
    reading or writing includes
  • Open the file, in a specific mode (see below)
  • Depending on the mode, perform some operations to
    that file
  • Close the file
  • Think about working on MS Word.
  • Modes of operation
  • r Read-only
  • w Write-only
  • Create a new file if not exist overwrite if
    exists
  • a Write-only
  • Write to the end of file
  • r Read and Write
  • w Write and Read
  • Create a new file if not exist overwrite if
    exists
  • a Read and Write
  • Read and Write to the end of file

14
Opening a File
  • Method fd fopen( fileName, mode)
  • (fd is a file handler which can be used later
    to represent the opened file.)

HTTP fopen fd fopen(http//www.google.com/,
r) FTP fopen fd fopen(ftp//userpass_at_ftp.
till.me.uk/file.txt, r) // only r and w
is supported Filesystem fopen fd
fopen(/var/www/html/cw3/user_X/session7/test.inc
, r)
15
Opening a File (cont.)
  • Error Handling What if something goes wrong?
  • If the URL is not correct
  • If the file does not exist
  • If we do not have permissions
  • etc.
  • Example

fd fopen(/var/www/html/cw3/user_X/session7/tes
t.inc, r) or die(Error opening the file!)
16
Reading a File
  • Reads the contents of the opened file
    (filePointer/handler) and assigns them as a
    string to a variable.
  • Method freadString fread( filePointer, length
    )
  • Example

// open the file filename /var/www/html/cw3/us
er_X/session7/test.inc fd fopen( filename,
r) fstring fread( fd, filesize(filename)
) // close the file fclose(fd)
17
Writing to a File
  • Writes the value of a variable to the open file
    (filePointer).
  • Method fwrite( filePointer, varName )
  • Example

filename test.inc // open the file fd
fopen( filename, w) stringToWrite Hello
world fwrite( fd, stringToWrite) // close
the file fclose(fd)
18
Closing a File
  • Closes the open file (filePointer). Its
    advisable to always close the files that open
    during the execution of the script. There is a
    limited amount of pointers and you may run out!
  • Method fclose( filePointer )

19
Exercise 7.2 Create a file
  • Write a number of strings in a file called
    test.txt and upload it to the same directory as
    where your PHP file is located.
  • Read the strings stored in that file and present
    them in the resulting web-page, one per line.
  • HINT You may add the delimiter \r\n which
    represents New Line at the end of each string
    when writing them to the file. When reading,
    parse them according to this delimiter!

http//uk2.php.net/manual/en/ref.filesystem.php
for details of File functions in PHP
Write a Comment
User Comments (0)
About PowerShow.com