IS 1181 IS 118 Introduction to Development Tools Chapter 4 String Manipulation and Regular Expressions
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…
IS 1183 Listing 4.1 <?php //create short variable names $name=$_POST['name']; $ =$_POST[' ']; $feedback=$_POST['feedback']; $toaddress = $subject = 'Feedback from web site'; $mailcontent = 'Customer name: '.$name."\n".'Customer '.$ ."\n"."Customer comments: \n".$feedback."\n"; $fromaddress = 'From: mail($toaddress, $subject, $mailcontent, $fromaddress); ?> Bob's Auto Parts - Feedback Submitted Feedback submitted Your feedback has been sent.
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])
IS 1185 A look at the code $mail content concatenates the customer name, 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.
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
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
IS 1188 Joining and splitting strings explode (string separator, string input) Splits up the input string into pieces based on the separator. $ ) separates an 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.’ ’; };
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);
IS 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
IS 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
IS 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
IS Putting it all together To check for a validly constructed address (p125): Means start at the beginning and it must have at least one letter, number, underscore, hyphen or dot. matches [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