In this tutorial, we shall cover categorised tutorials on string operations.
String Operations
1. Basics
- PHP – Create a string PHP Tutorial to define or initialize a string literal using single or double quotes.
- PHP – Create empty string PHP Tutorial to create an empty string using single quotes, double quotes, ticks, or String() constructor.
- PHP – Compare strings PHP Tutorial to compare if two given strings are equal to each other, if first string greater than other, or if first string smaller than other, using
str_cmp()
function. - PHP – Count number of words in string PHP Tutorial to count the number of words in a given string, by splitting the string into words and then finding the length of that split array.
- PHP – Get ASCII value of a character PHP Tutorial to get ASCII value of a given character using
ord()
function. - PHP – Iterate over characters of a string PHP Tutorial to iterate over each of the characters of the given string using a For loop.
- PHP – Iterate over words of a string PHP Tutorial to iterate over the words of a given string using foreach statement.
- PHP – Print string to console PHP Tutorial to print a string to output using
echo()
function. - PHP – String length PHP Tutorial to find the length of a string using
string.length
property. - PHP – Substring of a string PHP Tutorial to find the substring of a given string, based on starting index and length of substring, using
substr()
method.
2. Checks
- PHP – Check if string is empty PHP Tutorial to check if given string is empty or not using Equal-to comparison operator.
- PHP – Check if strings are equal PHP Tutorial to check if two strings are equal using equal to comparison operator.
- PHP – Check if strings are equal ignoring case PHP Tutorial to check if two strings are equal, with the condition that we ignore the case of characters, using equal to comparison operator.
- PHP – Check if string contains specified substring PHP Tutorial to check if a string contains given substring using
strpos()
function. - PHP – Check if string contains specific character
- PHP – Check if string starts with specific substring PHP Tutorial to check if a string starts with given prefix string using
strpos()
method. - PHP – Check if string ends with specific substring PHP Tutorial to check if a string ends with given suffix string using string.endsWith() method.
- PHP – Check if string starts with specific character
- PHP – Check if string starts with http PHP Tutorial to check if given string starts with the string “http”. This is usually used to check if given string URL starts with http or not.
- PHP – Check if string starts with a number
- PHP – Check if string starts with an uppercase PHP Tutorial to check if the first character in the string is an uppercase character or not, using
preg_match()
function. - PHP – Check if string starts with a lowercase PHP Tutorial to check if the first character in the string is a lowercase character or not, using
preg_match()
function. - PHP – Check if string ends with a punctuation PHP Tutorial to check if the last character in the string is a punctuation mark or not using
ctype_punct()
function. - PHP – Check if string ends with forward slash (/) character
- PHP – Check if string contains number(s)
- PHP – Check if string contains only alphabets PHP Tutorial to check if given string contains only alphabets, and no other characters, using
preg_match()
function. - PHP – Check if string contains only numeric digits PHP Tutorial to check if given string contains only numeric digits, and no other characters, using
preg_match()
function. - PHP – Check if string contains only lowercase PHP Tutorial to check if given string contains only lowercase characters using
ctype_lower()
function. - PHP – Check if string contains only uppercase PHP Tutorial to check if given string contains only uppercase characters using
ctype_upper()
function. - PHP – Check if string value is a valid number PHP Tutorial to check if contents of given string is a valid number using
is_numeric()
function. - PHP – Check if string is a float value PHP Tutorial to check if contents of given string is a floating point number using type casting and
strval()
function.
3. Updates
- PHP – Append a string with suffix string PHP Tutorial to append a given string at the end of the original string using string concatenation operator.
- PHP – Prepend a string with prefix string PHP Tutorial to append a given string at the start of the original string using string concatenation operator.
- PHP – Concatenate strings PHP Tutorial to concatenate two or more strings using string concatenation operator.
- PHP – Concatenate string and number PHP Tutorial to concatenate string and number into a string using
strval()
function and string concatenation operator. - PHP – Insert character at specific index in string PHP Tutorial to insert a character at specific index in a string using
subset_replace()
function. - PHP – Insert substring at specific index in string PHP Tutorial to insert a substring at specific index in a string using
subset_replace()
function. - PHP – Repeat string for N times PHP Tutorial to create a string that is repeated N times from the given string using
str_repeat()
function. - PHP – Trim spaces from edges of string PHP Tutorial to trim spaces around given string using
trim()
function. - PHP – Trim specific characters from edges of the string PHP Tutorial to delete specific characters from the leading and trialing edges of the given string using
trim()
function.
4. Split Operations
- PHP – Split string PHP Tutorial to split a given string by a delimiter string using
explode()
function. - PHP – Split string into words PHP Tutorial to split given string into words by using
explode()
function and spaces in the string as separators or delimiters. - PHP – Split string by comma PHP Tutorial to split string by comma separator using
explode()
function. - PHP – Split string by single space PHP Tutorial to split string by single space as delimiter/separator using
explode()
function. - PHP – Split string by new line
- PHP – Split string by any whitespace character PHP tutorial to split string by any whitespace character using
preg_split()
function. - PHP – Split string by one or more whitespace characters PHP tutorial to split string by one or more whitespace characters using
preg_split()
function. - PHP – Split string into substrings of specified length PHP Tutorial to split string into chunks of specific length using
str_split()
function.
5. Transformations
- PHP – Reverse a string PHP Tutorial to reverse a given string using
strrev()
function. - PHP – Convert string to lowercase PHP Tutorial to transform a given string to lowercase using
strtolower()
function. - PHP – Convert string to uppercase PHP Tutorial to transform a given string to uppercase using
strtoupper()
function. - PHP – Join elements of string array with separator string PHP Tutorial to join strings in array with specific separator string using
join()
function.
6. Delete Operations
- PHP – Delete first character of string PHP Tutorial to remove the first character of given string using
substr()
function. - PHP – Delete last character of string PHP Tutorial to remove the last character of given string using
substr()
function.
7. Search
- PHP – Count number of occurrences of substring in a string PHP Tutorial to count how many times a substring occurred in a string using
substr_count()
function. - PHP – Count number of overlapping occurrences of substring in a string
- PHP – Count alphabet characters in a string
- PHP – Find index of substring in a string PHP Tutorial to find the index of first occurrence of a substring (search string) in a string, using
strpos()
function. - PHP – Find index of last occurrence in a string
8. Matchings
- PHP – Find all the substrings that match a pattern PHP Tutorial to find all the substrings in the given string that match the specified regular expression, using
preg_match_all()
function. - PHP – Check if entire string matches a regular expression PHP tutorial to check if the entire string matches regular expression in the pattern using
preg_match()
function. - PHP – Check if string has a match for regular expression PHP Tutorial to check if there is at least one match for the regex in the string using
preg_match()
function.
9. Replacements
- PHP – Replace all occurrences of a substring in string PHP Tutorial to replace all the occurrences of specified substring in the string, with a replacement string using
str_replace()
. - PHP – Replace first occurrence in string PHP Tutorial to replace the first occurrence of specified search string, with a replacement string using
substr_replace()
. - PHP – Replace last occurrence in string PHP Tutorial to replace the last occurrence of specified substring, with a replacement string using
substr_replace()
.
10. Sorting
- PHP – Sort array of strings PHP Tutorial to sort given string array lexicographically based on the order they would come in a language dictionary.
- PHP – Sort strings in array based on length PHP Tutorial to sort given string array based on the length of the string elements using usort() function.
11. Formating
- PHP – Format string PHP Tutorial to format a string using
sprintf()
function. - PHP – Variable inside a string PHP Tutorial to specify a variable inside a string using curly braces.
12. Parsing
13. Conversions
- PHP – Convert CSV String into Array PHP Tutorial to convert given comma separated string into array, where the separated values are now elements in array.
- PHP – Convert string array to CSV string PHP Tutorial to convert a given array of strings to CSV string using
join()
function.
Summary
In this PHP Tutorial, we learned about strings and different operations that can be performed on strings.