Title: String functions
1String functions
- http//www.w3schools.com/php/php_ref_string.asp
2String Length
strlen() Returns the length of a string
str_word_count() Count the number of words in a string
3echo and print
echo() Outputs strings
print() Outputs a string
printf() Outputs a formatted string
sprintf() Writes a formatted string to a variable
fprintf() Writes a formatted string to a specified output stream
vprintf() Outputs a formatted string
vsprintf() Writes a formatted string to a variable
vfprintf() Writes a formatted string to a specified output stream
number_format() Formats a number with grouped thousands
4printf( )
- lt?php
- num1 3.141592654
- echo "num1ltbr/gt"
- printf("fltbr/gt", num1)
- printf(".2f", num1)
- // printf("fltbr/gt.2f", num1, num1)
- ?gt
5Split Join
explode() Breaks a string into an array
str_split() Splits a string into an array
implode() Returns a string from the elements of an array
join() Alias of implode()
6explode( )
- lt?php
- strW "HTML, CSS, JavaScript, PHP"
- arrW explode(", ",strW)
- echo "ltulgt"
- for (i0iltcount(arrW)i)
- echo "ltligtarrWilt/ligt"
- echo "lt/ulgt"
- ?gt
7implode( )
- lt?php
- arrW array("HTML", "CSS", "JavaScript",
"PHP") - echo "" . implode(" ",arrW) . ""
- ?gt
8ltrim( ), rtrim( ), trim( )
trim() Strips whitespace from both sides of a string
chop() Alias of rtrim()
rtrim() Strips whitespace from the right side of a string
ltrim() Strips whitespace from the left side of a string
lt?php hw " Hello World! " echo
"\"hw\"ltbr/gt" echo "ltrim \"" . ltrim(hw)
. "\"ltbr/gt" echo "rtrim \"" . rtrim(hw) .
"\"ltbr/gt" echo "trim \"" . trim(hw) .
"\"ltbr/gt" ?gt
9Replace, Convert
str_replace() Replaces some characters in a string (case-sensitive)
str_ireplace() Replaces some characters in a string (case-insensitive)
substr_replace() Replaces a part of a string with another string
str_pad() Pads a string to a new length
str_repeat() Repeats a string a specified number of times
str_shuffle() Randomly shuffles all characters in a string
strrev() Reverses a string
strtolower() Converts a string to lowercase letters
strtoupper() Converts a string to uppercase letters
ucfirst() Converts the first character of a string to uppercase
ucwords() Converts the first character of each word in a string to uppercase
addslashes() Returns a string with backslashes in front of predefined characters
stripslashes() Unquotes a string quoted with addslashes()
10Substring, Search
strlen() Returns the length of a string
substr() Returns a part of a string
strstr() Finds the first occurrence of a string inside another string (case-sensitive)
strchr() Finds the first occurrence of a string inside another string (alias of strstr())
stristr() Finds the first occurrence of a string inside another string (case-insensitive)
strrchr() Finds the last occurrence of a string inside another string
strpos() Returns the position of the first occurrence of a string inside another string (case-sensitive)
stripos() Returns the position of the first occurrence of a string inside another string (case-insensitive)
strrpos() Finds the position of the last occurrence of a string inside another string (case-sensitive)
strripos() Finds the position of the last occurrence of a string inside another string (case-insensitive)
strcmp() Compares two strings (case-sensitive)
strcasecmp() Compares two strings (case-insensitive)
11strpos( )
lt?php email _GET'mail'
//_POST'mail' if (pos strpos(email,
'_at_')) user substr(email,0,pos)
server substr(email, pos1)
echo "E-mail emailltbr/gt" echo "User
name userltbr/gt" echo "Server
serverltbr/gt" else echo
"lth2gtWrong E-mail!lt/h2gt" ?gt
http//163.22.32.190/php/str/strpos.php?mailycche
n_at_ncnu.edu.tw