130 likes | 208 Vues
Learn how to format, compare, match, and replace substrings using regular expressions in development tools. Explore joining, splitting, changing case, and more! Enhance your coding skills with practical examples and tips.
E N D
IS 118 Introduction to Development Tools Chapter 4 String Manipulation and Regular Expressions IS 118
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 118
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); ?> <html> <head> <title>Bob's Auto Parts - Feedback Submitted</title> </head> <body> <h1>Feedback submitted</h1> <p>Your feedback has been sent.</p> </body> </html> IS 118
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 118
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. IS 118
More Formatting • Converting text to HTML • nl2br () changes new lines to <br /> • 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 118
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 118
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.’<br />’; }; IS 118
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 118
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 118
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 118
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 118
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 IS 118