In this tutorial, we’ll discuss the str_split() and mb_str_split() functions to split a string into an array based on a specified length, strtok() function to split a string into smaller strings (tokens), and chunk_split() to split a string into smaller chunks.
Category: PHP
PHP is a scripting language that’s usually embedded or combined with HTML and has many excellent libraries that provide fast, customized access to DBMSs.
Count Characters and Words
Get byte values of all characters in a string and find how many times the byte-value occurred, how many times a character appears in a given string, receives unique characters from a string, etc. with count_chars() function. Also, count words in a string, convert a string into an array by words, get the frequency of all words in a string, find the position of each word in a string, etc. with str_word_count() function.
Repeat and Shuffle a String
How to use str_repeat() function to repeat a string a given number of times. You’ll also learn to shuffle a string to create a password generator.
All characters, even if they are not available on your keyboard, have a character code. PHP offers four functions to deal with these characters code: ord() and chr() to deal with single-byte characters; mb_ord and mb_chr() to deal with multi-byte characters. The chr() and mb_chr() convert the numeric value into the corresponding character; ord() and mb_ord() returns the numeric value for characters.
Find a given substring in the string
How to use the str_contains function to check if a string contains a given substring, str_starts_with function to check if a string starts with a given substring, and str_ends_with function to check if a string ends with a given substring.
Learn how to use strtr( ) function to translate characters and replace substrings in a given string.
Search all occurrences of a word or character and replace them with a replacement word or character.
Replace a portion of string by position
How to use substr_replace function to replace a string portion with another string by a specified position and length. You’ll also learn how to use array syntax over a string to access or modify an nth character by its index number.
Extract text before or after the match
How to retrieve the portion of text from a string with the help of strstr(), stristr(), strrchr(), and strpbrk() function.
In this tutorial, we’ll use the substr_count() function to determine how many times a substring occurs in a string.