Ch8 String.pptx
Ch8 String.pptx
String
▸ Strings in PHP are surrounded by either double quotation marks, or single quotation marks.
▸ Link: https://www.w3schools.com/php/php_string.asp
2
Double or Single Quotes?
▸ You can use double or single quotes, but you should be aware of the differences between the two.
▸ E.g. when there is a variable in the string, it returns the value of the variable:
3
String Functions
String Length
▸ strlen() function returns the length of a string.
5
Word Count
▸ str_word_count() function counts the number of words in a string.
6
Search For Text Within a
String
▸ strpos() function searches for a specific text within a string.
▸ If a match is found, the function returns the character position of the first match. If no match is
found, it will return FALSE.
0 1 2 3 4 5 6
▸ Search for the text “name” in the string “Hi my name is ali”
7
8
Upper Case
▸ strtoupper() function returns the string in upper case:
9
Lower Case
▸ strtolower() function returns the string in lower case:
10
Replace String
▸ str_replace() function replaces some characters with some other characters in a string.
11
Reverse a String
▸ strrev() function reverses a string.
12
Remove Whitespace
▸ trim() function removes any whitespace from the beginning or the end.
▸ Whitespace is the space before and/or after the actual text, and very often you want to remove this
space.
13
Convert String into Array
▸ explode() function splits a string into an array.
▸ The first parameter of the explode() function represents the "separator". The "separator" specifies
where to split the string.
▸ Example: Split the string into an array. Use the space character as separator:
14
String Concatenation
▸ To concatenate, or combine, two strings you can use the . Operator or double quotes.
15
Slicing Strings
▸ You can return a range of characters by using the substr() function.
▸ Specify the start index and the number of characters you want to return.
▸ Example: Start the slice at index 6 and end the slice 5 positions later.
▸ Slice to the End: By leaving out the length parameter, the range will go to the end
16
Example
17
Escape Character
▸ To insert characters that are illegal in a string, use an escape character.
18