PHP Strings - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

PHP Strings

Description:

PHP Strings – PowerPoint PPT presentation

Number of Views:189
Avg rating:3.0/5.0
Slides: 17
Provided by: RaghuRamak247
Category:
Tags: php | cowboys | strings

less

Transcript and Presenter's Notes

Title: PHP Strings


1
PHP Strings
  • A string can be single- or double-quoted.
  • An extensive set of functions for strings are
    provided
  • Manipulation with a regular expression,
  • Conversion between a string and an array,
  • Outputing by echo(), print(), printf(), and
    print_r(), and
  • Others

2
Single-Quoted String
  • No evaluation occurs for a single-quoted string.

age 20 echo I am age.\n echo
Single echo \Double\ echo
Double echo age gt 18 ? OK
I am age.\n (illegal) \Double\ Double OK
3
Double-Quoted String
  • Variables and escape sequences within a
    double-quoted
  • string are evaluated or replaced.

age 20 echo I am age.\n echo
Single\n echo \Double\\n echo
Double echo age gt 18 ? OK
I am 20. Single Double (illegal) OK
4
int strlen(string)
  • Returns the length of string string.

str 'Hello' len strlen(str) echo len
5
5
int strpos(string1, string2)
  • Returns the numeric position of string2 in
  • string1.

str My dog house.' position
strpos(str, dog) echo position
3
6
int strpos(str1, str2 ) Warning
  • Returns false if str2 is not a substring of
    str1.
  • Returns 0 if str2 is at the beginning of str1,
    so using if(strpos(str1, str2) 0) could
    cause an unreliable result.
  • if(strpos(str1, str2) 0) may be used.

7
string substr(string, start) Substring
Returns the string present in string starting
from location start until end of string.
  • str My dog.
  • str2 substr(str, 3)
  • echo str2
  • str3 substr(str1, 3, 2)
  • echo str3

dog. do
8
string strstr(string1, string2) and stristr()
Finds the location of string2 within string1, and
returns the remaining part of string1 from
that location. strstr is case-sensitive stristr
is not
  • str This is Department of EECS.
  • str2 strstr(str, Depart)
  • echo str2

Department of EECS.
9
array explode(delim, string)
Creates an array of the substrings in
string, separated by delim.
  • str Kelley Engineering Center
  • arr explode(" ", str)
  • echo arr0\narr1\narr2\n

Kelley Engineering Center
10
string implode(delim, array)
Creates a string out of the elements in
array, connected by delim.
  • arr array(S10', Wolf, 20)
  • string implode(', arr)
  • echo string

S10Wolf20
11
int ereg(pattern, string, array) Pattern
Matching I
Tests whether or not string matches
regular expression pattern.
  • ereg(boy', cowboys')
  • ereg('boy', cowboys')
  • ereg('cow', cowboys')
  • ereg(cow', 'cowboys')
  • ereg(boys', 'cowboys')

true false true false true
12
int ereg(pattern, string, array) Pattern
Matching II
Wild card characters.
  • ereg(.', cowboys')
  • ereg(. . . . . . .', cowboys')
  • ereg('c.s', cowboys')
  • ereg('c.s', cowgirls')
  • ereg('c.s', tomboys')
  • ereg(ab', aaabb')
  • ereg(ab', bbbb')
  • ereg(ab', ccbbb')
  • ereg(ab', aabbb')
  • ereg(ab', bbb')

true true true true false true true false true fal
se
13
int ereg(pattern, string, array) Pattern
Matching III
Specifying a range or set of valid characters.
  • ereg(a-z', cowboy')
  • ereg(a-z', cowboy123')
  • ereg(a-z0-9', cowboy123')
  • ereg(a-z6', cowboy')
  • ereg(a-z4', cowboy')
  • ereg(bcowy', cowboy')

true false true true false true
14
Extracting Substrings
Retrieving text in groups.
  • email minoura_at_eecs.orst.edu
  • ereg('(.)_at_(.)', email, arr)
  • echo Match arr0\n
  • echo Host arr1\n
  • echo Host arr2\n

Match minoura_at_eecs.orst.edu User minoura Host
eecs.orst.edu
15
Extracting Substrings
Retrieving text in groups.
  • email minoura_at_eecs.orst.edu
  • ereg('(.)_at_(.)', email, arr)
  • echo Match arr0\n
  • echo Host arr1\n
  • echo Host arr2\n

Match minoura_at_eecs.orst.edu User minoura Host
eecs.orst.edu
PHP String
16
Meta Characters for a Regular Expression
The following meta characters can be used in a
regular expression.
  • - start of line, or negation of
  • characters
  • - end of line
  • . - any character
  • range, or set of characters
  • () grouping
  • - any number of times
  • - at least once
  • ? - exactly once
  • n, m match n through m occurrences
Write a Comment
User Comments (0)
About PowerShow.com