Categories
PHP

Split, tokenize, and iterate a string

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.

Categories
PHP

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.

Categories
PHP

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.

Categories
PHP

Converting character to code and code to character

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.

Categories
PHP

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.

Categories
PHP

Replace characters or substrings with strtr()

Learn how to use strtr( ) function to translate characters and replace substrings in a given string.

Categories
PHP

Replace all occurrences of a string with str_replace()

Search all occurrences of a word or character and replace them with a replacement word or character.

Categories
PHP

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.

Categories
PHP

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.

Categories
PHP

How many times a substring occurs in a string

In this tutorial, we’ll use the substr_count() function to determine how many times a substring occurs in a string.