PHP String Manipulation Flashcards

1
Q

Q: What are the two ways to define a string in PHP?

A

A: Single quotes (‘) and double quotes (“).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Q: What is the difference between single and double quotes in PHP?

A

A: Double quotes parse variables and escape sequences, while single quotes do not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Q: How do you concatenate two strings in PHP?

A

A: Using the . operator, e.g., $str1 . $str2;.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Q: How do you find the length of a string in PHP?

A

A: Use the strlen() function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Q: How do you access a character at a specific index in a string?

A

A: Use array-like syntax, e.g., $string[0];.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Q: How do you convert a string to lowercase in PHP?

A

A: Use the strtolower() function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Q: How do you convert a string to uppercase in PHP?

A

A: Use the strtoupper() function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Q: How do you capitalise the first letter of a string?

A

A: Use the ucfirst() function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Q: How do you capitalise the first letter of each word in a string?

A

A: Use the ucwords() function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Q: How do you convert the case of a string to title case?

A

A: Use ucwords(strtolower($string));.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Q: How do you find the position of the first occurrence of a substring?

A

A: Use the strpos() function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Q: What function finds the last occurrence of a substring?

A

A: strrpos().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Q: How do you check if a string contains a substring?

A

A: Use strpos() and check if the result is not false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Q: How do you replace all occurrences of a substring in a string?

A

A: Use the str_replace() function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Q: How do you perform a case-insensitive string replacement?

A

A: Use the str_ireplace() function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Q: How do you extract a substring from a string?

A

A: Use the substr() function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Q: How do you get the first N characters of a string?

A

A: Use substr($string, 0, N);.

18
Q

Q: How do you get the last N characters of a string?

A

A: Use substr($string, -N);.

19
Q

Q: How do you remove a portion of a string?

A

A: Combine substr() and concatenation.

20
Q

Q: How do you pad a string to a specific length?

A

A: Use the str_pad() function.

21
Q

Q: How do you split a string into an array?

A

A: Use the explode() function.

22
Q

Q: How do you join an array into a string?

A

A: Use the implode() function.

23
Q

Q: How do you split a string into chunks of a specific size?

A

A: Use the str_split() function.

24
Q

Q: What function breaks a string into an array based on a regular expression?

A

A: preg_split().

25
Q

Q: How do you join array elements with a delimiter?

A

A: Use implode($delimiter, $array);.

26
Q

Q: How do you remove whitespace from the beginning and end of a string?

A

A: Use the trim() function.

27
Q

Q: How do you remove whitespace only from the beginning of a string?

A

A: Use the ltrim() function.

28
Q

Q: How do you remove whitespace only from the end of a string?

A

A: Use the rtrim() function.

29
Q

Q: How do you pad a string to the left?

A

A: Use str_pad($string, $length, $pad_string, STR_PAD_LEFT);.

30
Q

Q: How do you pad a string to the right?

A

A: Use str_pad($string, $length, $pad_string, STR_PAD_RIGHT);.

31
Q

Q: How do you encode a string for HTML output?

A

A: Use the htmlspecialchars() function.

32
Q

Q: How do you decode an HTML-encoded string?

A

A: Use the html_entity_decode() function.

33
Q

Q: How do you encode a string for use in a URL?

A

A: Use the urlencode() function.

34
Q

Q: How do you decode a URL-encoded string?

A

A: Use the urldecode() function.

35
Q

Q: How do you convert special characters to HTML entities?

A

A: Use the htmlentities() function.

36
Q

Q: How do you reverse a string in PHP?

A

A: Use the strrev() function.

37
Q

Q: How do you repeat a string multiple times?

A

A: Use the str_repeat() function.

38
Q

Q: How do you shuffle the characters of a string?

A

A: Use the str_shuffle() function.

39
Q

Q: How do you compare two strings for equality?

A

A: Use the strcmp() function.

40
Q

Q: How do you hash a string in PHP?

A

A: Use the hash() function, e.g., hash(‘sha256’, $string);.