0% found this document useful (0 votes)
4 views18 pages

Ch8 String.pptx

Uploaded by

otrdthe6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views18 pages

Ch8 String.pptx

Uploaded by

otrdthe6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Chapter 8

String

By: Asst. Lect. Montadhar Moslem


PHP Strings
▸ A string is a sequence of characters, like "Hello world!".

▸ 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.

▸ Double quoted strings perform action on special characters.

▸ 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.

▸ Replace the text “ali" with “Ahmed":

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.

▸ Note: The separator is required.

▸ 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.

▸ Note The first character has index 0.

▸ 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.

▸ An escape character is a backslash \ followed by the character you want to insert.

18

You might also like