ch.9b string manipulation
Post on 30-May-2018
223 Views
Preview:
TRANSCRIPT
-
8/9/2019 Ch.9b String Manipulation
1/13
String Manipulation
By Tony Chun Kuen WONG
Working with String
Formatting string
Joining and Splitting strings
Matching and replacing substrings withstring functions
-
8/9/2019 Ch.9b String Manipulation
2/13
trim( ); Strips whitespace from the start and end of a
s r ng, an re urns e resu ng s r ng. s r ps Newlines (\n), Carriage return (\r) , Horizontal tab (\t)
, ,
ltrim ( );
start (or left)
cho ; same as above but remove whitespaces from the
end (or right)
Formatting Strings for Presentation
nl2br ( ) function
It takes a string as parameter and replacesall the newlines in it with the HTML
tag -
8/9/2019 Ch.9b String Manipulation
3/13
Formatting Strings for Printing
printf ( ); Prints a formatted string to the browser
sprintf ( );
Returns a formatted string
String Case Functions
e.g. Define String variable $subject = Feedback from web site;
strtoupper($subject); FEEDBACK FROM WEB SITE
ucfirst($subject); Feedback from web site
ucwords($subject); Feedback From Web Site
http://isem03.hkbu.edu.hk/~isemuser20/string/generic_form.html
-
8/9/2019 Ch.9b String Manipulation
4/13
Formatting Strings for Storage
AddSlashes ( ); Used to add \ (blakslash) in order to make
the MySQL database can understand theliteral special character like ( ; ; \ ,)
StripSlashes ( );
Opposite to the above function
Joining and Splitting Strings
explode ( );
s unc on a e a s r ng npu an sp s into pieces on a specified separatorstring. Theieces are returned in an arra
implode ( ); join ( ); Reverse the effect of ex lode
strtok ( ); Like ex lode but Token a strin once a time.
substr ( ); Access a substrin between iven start and
end points of a string
-
8/9/2019 Ch.9b String Manipulation
5/13
ompar ng r ngs
strcmp ;
The function will compare two string. If they are equal, it.
in lexicographic order, it will return a number greaterthan 0. Otherwise, it will return a number less than 0.This function is case sensitive.
strcasecmp ( ); Same as strcmp ( ) ; not case sensitive
The below two compare string in natural ordering e.g. 2 is less than 12 in the following two
strnatcmp ( ); strnatcasecmp ( );
est ng tr ng engt
strlen ( );
Matching and Replacing Substrings
strstr strchr Use to find a string/character inside a string
If exact match is found, the function returns fromt e rst occurrence, ot erw se t return a se;
stristr ( );
Same as above but not case sensitive
strrchr ( ); It will return from the last occurrence onwards.
-
8/9/2019 Ch.9b String Manipulation
6/13
Finding the Position of a Substring
Both functions operate in a similar fashion to, ,substring, they return the numerical position
. strpos ( );
Return the first position
strrpos ( );
Return the last position
We can specify the offset before searching
Replacing Substring
str_replace ( );
_ new_needle, string haystack);
This function will replace all thens ances o nee e n ays ac w
new_needle
-
8/9/2019 Ch.9b String Manipulation
7/13
ep ac ng u s r ng
substr_replace ( ); string substr_replace(string string, string
, ,
This function will replace part of the stringwith thestr ng rep acement. c parts s rep ace epen supon the values of the startand the optional lengthparameters.
length
0 inserted into the string
os t ve rep ace t e exact num er o c aracters
Negative represents the point at which youd like to,
the end of the string
Regular Expression
Used for examining text and modifying
text. They enable us to search foratterns within a strin extractin
flexibly and precisely. Be warned that,
are also slower than the more basics r ng unc on.
-
8/9/2019 Ch.9b String Manipulation
8/13
Matching regular expressions in PHP is
comparsion because you are matching
string
The string Tony matches the regular
regular expressions on, o and so on.
Character Sets and Classes
. character except a new line (\n)
.at matches the strings cat, sat,and mat, among others
-
8/9/2019 Ch.9b String Manipulation
9/13
xpress on n t e square rac etsmatches only a single character
[a-z] limit to character[aeiou] limit to vowel
- -
[^a-z] not between a-z
[[:alnum:]] Alphanumeric characters
[[:alpha:]] Alphabetic characters
[[:lower:]] Lowercase letters
[[:upper:]] Uppercase letters[[:digit:]] Decimal digits
:x g : eax ec ma g s
[[:punct:]] Punctuation
[[:space:]] Whitespace characters
[[:print:]] All printable characters
for space
-
8/9/2019 Ch.9b String Manipulation
10/13
epet t on
Symbols means the pattern can berepeated zero or more times
+ Symbols means that the pattern can
The symbol should appear directly after thepart of expression that it applied to
[[:alnum:]]+ means at least one
alphanumeric character.
Subexpressions
Searching for at least one of these
s r ngs o owe y exac y one o ose
e.g.*
, ,
very large, and so on.
-
8/9/2019 Ch.9b String Manipulation
11/13
Counted Subexpressions
Specify how many times something can be
curly braces { }.
(very){1, 3}
matc es
very, very very and very very very
(very) {2} matches very very
of a String
You can specify whether a particular
su express on s ou appear a estart, the end, or both.
^ start
o ma c es o a e s ar o a s r ng
com$ matches com at the end of the string
-
8/9/2019 Ch.9b String Manipulation
12/13
Branching
Matching through a set of choice
. .
(com)|(edu)|(net) match com, edu, or net
h r r
If we want to match one of the special
characters, you must put a slash (\) infront of it.
-
8/9/2019 Ch.9b String Manipulation
13/13
Regular Expression commands
ereg_replace Replace regular expressioneregi_replace Replace regular expression caseinsensitiveereg ase nsens t ve regu ar express on matcsplit Split string into array by regular expression
case insensitive
sql_regcase Make regular expression for casensens ve ma c
. .
top related