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.

Categories
PHP

Find positions of a substring in a string

How to find the position of the first or last occurrence of a substring in a string. You’ll also learn how to find the all positions of a substring in a string (or a multibyte string).

Categories
PHP

Extract a specific portion of string by position

Use the substr() function to extract a portion of a string. Use mb_substr() function instead if you are dealing with a multibyte string.

Categories
PHP

Replace Newlines with BR Tag and Trim Whitespaces

How to remove leading and trailing whitespace from a string and how to replace line breaks in a string with <br> tags.

Categories
PHP

Padding Strings

Pad a string on the left, on the right, or on both the left and the right using str_pad() and printf() functions.