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

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Form Validation CS What is form validation?  validation: ensuring that form's values are correct  some types of validation:  preventing blank.
Data Manipulation & Regular Expressions CSCI 215.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Validating user input Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
Form Validation CS What is form validation?  validation: ensuring that form's values are correct  some types of validation:  preventing blank.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
. If the PHP server is an server or is aware of which server is the server, then one can write code that s information. –For example,
Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies Knowledge Engineering : Systems Integration : Web.
Last Updated March 2006 Slide 1 Regular Expressions.
Chapter 4 Handling User Input PHP Programming with MySQL 2nd Edition
PHP : Hypertext Preprocessor
1 Chapter 6 – Creating Web Forms and Validating User Input spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information.
(c) Manzur Ashraf, Short course, KFUPM PHP & MySQL 1 Basic PHP Class 2.
Strings and Arrays. String Is a sequence of characters. Example: “hello”, “how are you?”, “123”,and are all valid string values.
Strings IDIA 618 Spring 2013 Bridget M. Blodgett.
Server-Side Validation Jayden Bryant. What is Server-Side Validation?  Validation of form input done on the server, not the web browser program //Validate.
Tutorial 14 Working with Forms and Regular Expressions.
Faculty of Sciences and Social Sciences HOPE JavaScript Validation Regular Expression Stewart Blakeway FML
PHP Workshop ‹#› Data Manipulation & Regex. PHP Workshop ‹#› What..? Often in PHP we have to get data from files, or maybe through forms from a user.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
Finding the needle(s) in the textual haystack
Strings in PHP Working with Text in PHP Strings and String Functions Mario Peshev Technical Trainer Software University
Website Development with PHP and MySQL Saving Data.
Regular Expressions.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
String Manipulation. Strings have their own properties and methods, just like a textbox or label or form does.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
Introduction to Web Programming. Introduction to PHP What is PHP? What is a PHP File? What is MySQL? Why PHP? Where to Start?
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
Regular Expressions for PHP Adding magic to your programming. Geoffrey Dunn
Regular Expressions What is this line all about? while (!($search =~ /^\s*$/)) { It’s a string search just like before, but with a huge twist – regular.
Introduction to Database using Microsoft Access 2013 Part 6.1 November 18, 2014.
Operators Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
Slide 1 PHP Predefined Functions ITWA113. Murach’s ASP.NET 3.5/C#, C1© 2008, Mike Murach & Associates, Inc. Slide 2 PHP Predefined Functions Objectives.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Arrays Strings and regular expressions Basic PHP Syntax CS380 1.
Gollis University Faculty of Computer Engineering Chapter Five: Retrieval, Functions Instructor: Mukhtar M Ali “Hakaale” BCS.
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
Regular Expressions.
Regular Expressions Upsorn Praphamontripong CS 1110
String Methods Programming Guides.
CS 330 Class 7 Comments on Exam Programming plan for today:
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
C Characters and Strings
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Intro to PHP & Variables
String functions
Data Manipulation & Regex
Web Programming Language
JavaScript: Objects.
Introduction to Computer Science
Python Strings.
Lexical Elements & Operators
String functions
C Characters and Strings
Presentation transcript:

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