PHP Strings Primer - Substrings (and searching) (Page 23 of 37 ) Many varied tasks can be accomplished by using substrings. We will define a substring as a string that makes up any portion of another string. This could be as small as a single character, or as large as the entire length of the string. In this section, we will examine different methods for finding substrings, replacing those substrings, and other related duties. First, let's take a look at the functions covered in this section. substr(string, start [, length]) -Using this function, we can extract parts of a string to store in another string, or utilize in other functions.substr_count(haystack, needle) -This function allows us to count the number of times a substring occurs within a string.str_replace(haystack, replacement, needle) -With this function, we can replace all occurrences of a substring within a string with a different string.substr_replace(string, replacement, start [, length]) -This is a different method of replacing portions of a string based on positions rather than searching for a substring.strstr(haystack, needle) -Also called 'strchr', this function will find the first occurrence of a substring and return the portion of the string from that occurrence on.stristr(haystack, needle) -This provides the same functionality as 'strstr', but searches in a case-insensitive manner.strrchr(haystack, needle) -This function is identical to strstr', except that it finds the last occurrence of a substring instead of the first.strpos(haystack, needle) -This function is similar to the 'strstr' function, with the difference being that an integer specifying where the first occurrence begins is returned rather than a string.strrpos(haystack, needle) -An integer specifying where the last occurrence of a character is found within a string is returned by this function.strspn(string1, string2) -This function will return the number of sequential characters at the beginning of a string that match the characters supplied in the second parameter.strcspn(string1, string2) -Rather than providing the number of characters that match the second parameter, this function returns the number of sequential characters that do not match. Next: Extracting Substrings >>
More Programming Basics Articles More By Matt Wade |