PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,

Slides:



Advertisements
Similar presentations
Regular Expressions (in Python). Python or Egrep We will use Python. In some scripting languages you can call the command “grep” or “egrep” egrep pattern.
Advertisements

Text manipulation  Suppose you want to build a web-page which will always contain the latest sports headlines collected from several newspaper websites.
Chapter 14 Perl-Compatible Regular Expressions Part 1.
Data Manipulation & Regular Expressions CSCI 215.
Regular Expression (1) Learning Objectives: 1. To understand the concept of regular expression 2. To learn commonly used operations involving regular expression.
Regular Expressions in Java. Namespace in XML Transparency No. 2 Regular Expressions Regular expressions are an extremely useful tool for manipulating.
Regular Expressions in Java. Regular Expressions A regular expression is a kind of pattern that can be applied to text ( String s, in Java) A regular.
Validating user input Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
IS 1181 IS 118 Introduction to Development Tools Chapter 4 String Manipulation and Regular Expressions.
Scripting Languages Chapter 8 More About Regular Expressions.
UNIX Filters.
Regex Wildcards on steroids. Regular Expressions You’ve likely used the wildcard in windows search or coding (*), regular expressions take this to the.
Regular Expression A regular expression is a template that either matches or doesn’t match a given string.
Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies Knowledge Engineering : Systems Integration : Web.
REGULAR EXPRESSIONS CHAPTER 14. REGULAR EXPRESSIONS A coded pattern used to search for matching patterns in text strings Commonly used for data validation.
Last Updated March 2006 Slide 1 Regular Expressions.
1 Perl Regular Expressions in SAS 9 Ruth Yuee Zhang, CFE Jan 10, 2005.
Sys.Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 18: Regular Expressions in PHP.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Characters In Java, single characters are represented.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
Finding the needle(s) in the textual haystack
RegExp. Regular Expression A regular expression is a certain way to describe a pattern of characters. Pattern-matching or keyword search. Regular expressions.
1 DIG 3134 – Lecture 10: Regular Expressions and preg_match in PHP and Validating Inputs Michael Moshell University of Central Florida Internet Software.
Regular Expressions Regular expressions are a language for string patterns. RegEx is integral to many programming languages:  Perl  Python  Javascript.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
Perl and Regular Expressions Regular Expressions are available as part of the programming languages Java, JScript, Visual Basic and VBScript, JavaScript,
Python Regular Expressions Easy text processing. Regular Expression  A way of identifying certain String patterns  Formally, a RE is:  a letter or.
1 CSC 594 Topics in AI – Text Mining and Analytics Fall 2015/16 4. Document Search and Regular Expressions.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
PHP with Regular Expressions Web Technologies Computing Science Thompson Rivers University.
BY Sandeep Kumar Gampa.. What is Regular Expression? Regex in.NET Regex Language Elements Examples Regular Expression API How to Test regex in.NET Conclusion.
Regular Expressions in PHP. Supported RE’s The most important set of regex functions start with preg. These functions are a PHP wrapper around the PCRE.
PHP| SCK3633 Web Programming | Jumail, FSKSM, UTM, 2006 | Last Updated March 2006 Slide 1 Regular Expressions.
 2003 Jeremy D. Frens. All Rights Reserved. Calvin CollegeDept of Computer Science(1/8) Regular Expressions in Java Joel Adams and Jeremy Frens Calvin.
REGEX. Problems Have big text file, want to extract data – Phone numbers (503)
Overview A regular expression defines a search pattern for strings. Regular expressions can be used to search, edit and manipulate text. The pattern defined.
Prof. Alfred J Bird, Ph.D., NBCT Door Code for IT441 Students.
C# Strings 1 C# Regular Expressions CNS 3260 C#.NET Software Development.
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.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming regular expressions.
JavaScript, Part 2 Instructor: Charles Moen CSCI/CINF 4230.
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.
12. Regular Expressions. 2 Motto: I don't play accurately-any one can play accurately- but I play with wonderful expression. As far as the piano is concerned,
CS346 Regular Expressions1 Pattern Matching Regular Expression.
GREP. Whats Grep? Grep is a popular unix program that supports a special programming language for doing regular expressions The grammar in use for software.
PHP’s Regular Expression Functions (Perl Compatible) Examples taken from: Beginning PHP 5 and MySQL 5 From Novice to Professional.
Flex Fast LEX analyzer CMPS 450. Lexical analysis terms + A token is a group of characters having collective meaning. + A lexeme is an actual character.
Powerpoint Templates Page 1 Powerpoint Templates GROUP 8:REGULAR EXPRESSION GURU BESAR: PN. SARINA SULAIMAN CIKGU-CIKGU: 1.CIKGU NENI 2.CIKGU
CSC 2720 Building Web Applications PHP PERL-Compatible Regular Expressions.
1 DIG 3563: Lecture 2a: Regular Expressions Michael Moshell University of Central Florida Information Management.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Regular Expressions.
Java Script Pattern Matching Using Regular Expressions.
Strings and Related Classes String and character processing Class java.lang.String Class java.lang.StringBuffer Class java.lang.Character Class java.util.StringTokenizer.
Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.
Regular Expressions /^Hel{2}o\s*World\n$/ SoftUni Team Technical Trainers Software University
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
An Introduction to Regular Expressions Specifying a Pattern that a String must meet.
Chapter 4 © 2009 by Addison Wesley Longman, Inc Pattern Matching - JavaScript provides two ways to do pattern matching: 1. Using RegExp objects.
Variable Variables A variable variable has as its value the name of another variable without $ prefix E.g., if we have $addr, might have a statement $tmp.
Pattern Matching: Simple Patterns. Introduction Programmers often need to scan a file, directory, etc. for a specific substring. –Find all files that.
OOP Tirgul 11. What We’ll Be Seeing Today  Regular Expressions Basics  Doing it in Java  Advanced Regular Expressions  Summary 2.
Python Pattern Matching and Regular Expressions Peter Wad Sackett.
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.
Lecture 19 Strings and Regular Expressions
Perl-Compatible Regular Expressions Part 1
Lecture 9 Shell Programming – Command substitution
Embedded PHP in HTML 5 & Strings in PHP
STRING MANUPILATION.
Presentation transcript:

PHP Using Strings 1

Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle, mixed $new_needle, mixed $haystack[, int $count] ) Replaces all instances of $needle in $haystack with $new_needle Returns a new string = a copy of $haystack with all replacements made $count has to be a variable (lhs) that will be set to the number of replacements made $needle, $new_needle, $haystack can be strings or arrays Matching & replacing with string functions 2

Feeback.html s/feedback.html s/feedback.html s/processfeedback_v2_php.pdf s/processfeedback_v2_php.pdf 3

The match function is int preg_match(string $pattern, string $str [, array &$matches [, …]]) 1 st argument is the pattern 2 nd argument is the subject string Returns number of matches found: 1 (true) if there is a match – it stops at the first match found! 0 (false) otherwise An optional 3 rd argument will capture the actual matched string and the parenthesized subpatterns, if any. int preg_match_all(…) Use to find all matches to the regular expression 4 Regular Expressions

Character Classes [A-Z]uppercase letter [a-z]lowercase letter [0-9]digit {5}exactly five {3,}three or more {1,4}one to four 5

Metacharacters ^beginning of string $end of string 6

Regular Expression Username – 5 to 10 lowercase letters? $pattern = '/^[a-z]{5,10}$/'; if (!preg_match($pattern, $username)) { echo " $username is invalid. "; } 7

Regular Expressions Social Security Number $pattern = '/^[0-9]{3}-[0-9]{2}-[0-9]{4}$/'; if (!preg_match($pattern, $ssn)) { echo " $ssn is invalid. "; } 8

Character classes \dany digit \Dany non-digit \wAny letter, number, or underscore \WAnything that is not a letter, number, or underscore \swhitespace (space, tab, new line, etc.) \SAn y non-whitespace 9

Regular Expressions Social Security Number $pattern = '/^\d{3}-\d{2}-\d{4}$/'; if (!preg_match($pattern, $ssn)) { echo " $ssn is invalid. "; } 10

Metacharacters [0-9]** = zero or more [A-Z]++ = one or more.. = match any character ([A-Z]\. )?? = either zero or one 11

Regular Expressions Last name: Frank $pattern = '/^[A-Z][a-z]+$/'; if (!preg_match($pattern, $last)) { echo " $last is invalid. "; } 12

Regular Expressions Name: Charles Frank or Charles E. Frank $pattern = '/^[A-Z][a-z]+ ([A-Z]\. )?[A-Z][a-z]+ $/'; if (!preg_match($pattern, $name)) { echo " $name is invalid. "; } 13