Presentation is loading. Please wait.

Presentation is loading. Please wait.

IS 1181 IS 118 Introduction to Development Tools Chapter 4 String Manipulation and Regular Expressions.

Similar presentations


Presentation on theme: "IS 1181 IS 118 Introduction to Development Tools Chapter 4 String Manipulation and Regular Expressions."— Presentation transcript:

1 IS 1181 IS 118 Introduction to Development Tools Chapter 4 String Manipulation and Regular Expressions

2 IS 1182 Things to Cover Formatting strings Joining and splitting strings Comparing strings Matching and replacing substrings Using regular expressions This will be done using a Smart Form…

3 IS 1183 Listing 4.1 <?php //create short variable names $name=$_POST['name']; $email=$_POST['email']; $feedback=$_POST['feedback']; $toaddress = 'feedback@example.com'; $subject = 'Feedback from web site'; $mailcontent = 'Customer name: '.$name."\n".'Customer email: '.$email."\n"."Customer comments: \n".$feedback."\n"; $fromaddress = 'From: webserver@example.com'; mail($toaddress, $subject, $mailcontent, $fromaddress); ?> Bob's Auto Parts - Feedback Submitted Feedback submitted Your feedback has been sent.

4 IS 1184 Listing facts Normally would check that all fields are filled – this does not! It also assumes that all fields are okay without extra spaces – Not good Uses mail function which is:  Bool mail (string to, sy=tring subject, string message, string [addit headers], string [addit parms])

5 IS 1185 A look at the code $mail content concatenates the customer name, email and comments into a single string This leads us to formatting strings Trimming:  trim () – strips off white space from beginning and end  Ltrim () and rtrim () – strips off white spaces from the left or the right.

6 IS 1186 More Formatting Converting text to HTML  nl2br () changes new lines to  sprintf returns a formatted string  printf () sends a formatted string to the browser  General format is (string format [, mixed args…])  See table 4.1 for conversion codes  Ex: printf(“total amount of order is %.2f (with shipping %.2f) “, $total, $total_Shipping) %.2f is format symbol (%), 2 decimals floating point

7 IS 1187 More Formatting Changing case  Srtrtoupper ($subject) changes to uppercase  Strtolower ($subject) to lower case  ucfirst ($subject) first letter to uppercase  Uwords( $subject) first letter of each word to uppercase  Addslashes() and stripsslashes() to take out or add slashes – often for database work

8 IS 1188 Joining and splitting strings explode (string separator, string input)  Splits up the input string into pieces based on the separator. explode(‘@’, $email) separates an email address into two parts strtok (string input, string separator) Splits up input string into pieces but one piece at a time $token = strtok($feedback, ‘ ‘); While ($token !=‘’) { $token – strtok(‘ ‘); echo $token.’ ’; };

9 IS 1189 More strings functions Substr ( string string, int start[, int length]);  Substr($test, 0, 4); Returns the first four characters Finding strings  strstr(), strchr(), strrchr() strstr( string haystack, string needle)  Strpos (), strrpos() – similar to strstr() Replacing strings  str_replace(), substr_replace() $feedback = str_replace($offcolor, ‘%!@*’, $feedback);

10 IS 11810 Regular Expressions When you want to do more than simple matches done so far we use regular expressions  It describes a pattern in a piece of text  It can use wildcards, (.) to match to a single character.at => cat, sat, mat, #at would all match  Can control the matching [a-z]at says match any lowercase letter [a-zA-Z] says match any lower or upper case letter [^a-z]at means anything BUT lowercase a-z

11 IS 11811 Pre-defined character classes – p122  [[:alnum:]] Alphanumeric  [[:alpha:]] Alpha only  [[:lower:]] Lower case  [[:upper:]] Upper case  [[:digit:]] Decimal digits  [[:xdigit]] Hexadecimal digits  [[:punct:]] Punctuation  [[:blank:]] Tabs and spaces  [[:space:]] White space characters

12 IS 11812 Other Repetition: [[:alpha:]]+ means at least one alpha character Sub-expressions: [very]*large matches Large, very large, very very large and more Counted Sub-expressions: [very](1, 3) very, very very, very very very Special characters see table 4.4

13 IS 11813 Putting it all together To check for a validly constructed email address (p125): ^[a-zA-Z0-9_\-.]+@[a-zA-Z0-9_\-.]+\.[a-zA-Z0-9_\-.]+$  Means start at the beginning and it must have at least one letter, number, underscore, hyphen or dot.  @ matches the @  [a-zA-Z0-9_\-.]+ matches then the first part of the host name  The. Matches.  [a-zA-Z0-9_\-.]+$ matches the rest of the name Regular expression are not as efficient as string expressions but are more flexible and powerful


Download ppt "IS 1181 IS 118 Introduction to Development Tools Chapter 4 String Manipulation and Regular Expressions."

Similar presentations


Ads by Google