PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with...
-
Upload
merry-james -
Category
Documents
-
view
215 -
download
0
Transcript of PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with...
![Page 1: PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649e8f5503460f94b92905/html5/thumbnails/1.jpg)
PHP Using Strings
1
![Page 2: PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649e8f5503460f94b92905/html5/thumbnails/2.jpg)
Replacing substrings (replace certain parts of a document template; ex
<name> 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_needleReturns 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
![Page 3: PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649e8f5503460f94b92905/html5/thumbnails/3.jpg)
Feeback.htmlhttp://www.nku.edu/~frank/csc301/Examples/PHP_String
s/feedback.htmlhttp://www.nku.edu/~frank/csc301/Examples/PHP_String
s/processfeedback_v2_php.pdf
3
![Page 4: PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649e8f5503460f94b92905/html5/thumbnails/4.jpg)
The match function isint preg_match(string $pattern, string $str [, array &$matches [, …]])
1st argument is the pattern2nd argument is the subject stringReturns number of matches found:
1 (true) if there is a match – it stops at the first match found! 0 (false) otherwise
An optional 3rd 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
![Page 5: PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649e8f5503460f94b92905/html5/thumbnails/5.jpg)
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
![Page 6: PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649e8f5503460f94b92905/html5/thumbnails/6.jpg)
Metacharacters^ beginning of string$ end of string
6
![Page 7: PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649e8f5503460f94b92905/html5/thumbnails/7.jpg)
Regular ExpressionUsername – 5 to 10 lowercase letters?
$pattern = '/^[a-z]{5,10}$/';
if (!preg_match($pattern, $username))
{
echo "<p>$username is invalid.</p>";
}
7
![Page 8: PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649e8f5503460f94b92905/html5/thumbnails/8.jpg)
Regular ExpressionsSocial Security Number 123-45-6789
$pattern = '/^[0-9]{3}-[0-9]{2}-[0-9]{4}$/';
if (!preg_match($pattern, $ssn))
{
echo "<p>$ssn is invalid.</p>";
}
8
![Page 9: PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649e8f5503460f94b92905/html5/thumbnails/9.jpg)
Character classes\d any digit\D any non-digit\w Any letter, number, or underscore\W Anything that is not a letter, number, or underscore\s whitespace (space, tab, new line, etc.)\S An y non-whitespace
9
![Page 10: PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649e8f5503460f94b92905/html5/thumbnails/10.jpg)
Regular ExpressionsSocial Security Number 123-45-6789
$pattern = '/^\d{3}-\d{2}-\d{4}$/';
if (!preg_match($pattern, $ssn))
{
echo "<p>$ssn is invalid.</p>";
}
10
![Page 11: PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649e8f5503460f94b92905/html5/thumbnails/11.jpg)
Metacharacters[0-9]* * = zero or more[A-Z]+ + = one or more. . = match any character([A-Z]\. )? ? = either zero or one
11
![Page 12: PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649e8f5503460f94b92905/html5/thumbnails/12.jpg)
Regular ExpressionsLast name: Frank
$pattern = '/^[A-Z][a-z]+$/';
if (!preg_match($pattern, $last))
{
echo "<p>$last is invalid.</p>";
}
12
![Page 13: PHP Using Strings 1. Replacing substrings (replace certain parts of a document template; ex with client’s name etc) mixed str_replace (mixed $needle,](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649e8f5503460f94b92905/html5/thumbnails/13.jpg)
Regular ExpressionsName: Charles Frank or Charles E. Frank
$pattern = '/^[A-Z][a-z]+ ([A-Z]\. )?[A-Z][a-z]+ $/';
if (!preg_match($pattern, $name))
{
echo "<p>$name is invalid.</p>";
}
13