Online
 
Friday, 09 January 2009
 
 
More article:
Related Content:

Your Ad Here

Implementing Arrays
 
Article Index
Implementing Arrays
Page 2
Page 3
Page 4
Page 5
Page 6
Page 7
Page 8
Page 9
Page 10

With all the initialization complete, the next step in the script is to cycle through each individual character within the message to encode using a for() loop starting from 0 to the length returned by the strlen() function. The strlen() function returns the length of the provided string (strings are discussed in detail in ). Because a number of cryptogram-generated characters don't translate (punctuation, whitespace, and so on) it is necessary to check to see that each character in the message being encoded exists prior to attempting to encode it. To do so, use the in_array() function to ensure that the character exists within the $cryptogram lookup table. If there is no translation available for the particular character (meaning in_array() returned false) it is passed straight into the encoded message stored in the $encoded variable. Otherwise, the character is translated based on the $cryptogram array. However, because the $cryptogram array's keys are not characters (recall that they are integer values 025) the $alphabet array is used to retrieve the particular integer value for the given character.

After the message is processed character-by-character, $encoded contains the complete cryptogram for the message (which is then displayed to the client). Because this script generates a completely new cryptogram every time it is executed, it is impossible to display the exact output. However, when this script was executed, my output was the following:

DPYM YM RJ MHAXB MXUBXD RXMMOEX!

Converting from Strings to Arrays and Back

As introduced in , you can use a number of methods for processing strings in PHP. One very common use of arrays is processing a list of items represented as a string, such as the following:

John Coggeshall, Max Smith, Mary Johnston

When processing such a list, PHP provides a function that allows you to tokenize (separate) string values by a constant separatorthe explode() function with the following syntax:

explode($separator, $string [, $limit]);

$separator is the string that will represent the separator that $string will be broken into. Optionally, you can also pass the $limit parameter, which represents the maximum number of splits to occur. When executed, the explode() function breaks a string into individual array elements using the value of $separator to indicate where each split will occur. Using our earlier string list, to break apart each name the comma character (,) would be used as shown in


Tags: Add more tags...,
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: Simple PHP, Pear, Easy PHP, PHP Tutorial, PHP MySQL, XSLT, Sap Tutorial, CSS Tutorial, XSL FO Java, SQL Tutorial.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Natural 378
Statistic


Last Post

 
Top! Top!