String and regular expression functions in PHP

String statistics

Calculating string length comes up often in PHP development; the function you need is strlen(). For counting the words in a string there’s str_word_count() (it only works on ASCII English words)

Removing spaces

In many cases PHP development has to take user convenience into account — for example, what if someone accidentally types an extra space when entering their username? Then you use the space-clearing functions: ltrim() removes the spaces at the left end of the string, rtrim() removes the spaces at the right end of the string, and trim() removes the leading and trailing spaces on both sides at the same time. You can look for the pattern in these three functions: trim clears both the left and the right, so in ltrim the l is like left, meaning it clears on the left. Likewise for the right.

Splitting strings

String splitting is commonly used in PHP, namely the explode() and strtok() functions. The implode() function joins things into a single string according to a certain separator. implode() is also aliased as join() and the functionality is the same. Among these, explode() is used more, mostly for splitting email addresses, URLs, file paths and so on. Compared with explode(), strtok() can remember the position of the newly split string in the original string so it can keep splitting.

Truncating strings

Note: each Chinese character takes up 2 characters. In PHP development you also run into cutting strings short, and that calls for the substr() function. The target string is the variable name of some string variable, and both the start position and the cut length are integers. If both are positive, the start position integer must be smaller than the length integer, otherwise the function returns false. If the cut length is negative, it means: starting from the start position and going forward, all characters except for the last (length) characters counted from the end of the target string. Format: substr(target string, start position, cut length)

Replacing strings

Usually in actual PHP development some parts of strings are the same, and to improve the site’s efficiency we need to replace them and make a call. That’s the substr_replace() function. Format: substr_replace(target string, replacement string, start position, replacement length)

Searching strings

PHP generates a whole pile of strings during development or encryption, and sometimes we need to locate a string, which requires the search function strstr(). The strstr() function is not case sensitive. Format: strstr(target string, characters to find)

Regular expressions

Replacing strings with regular expressions

When writing code, a website often needs to exchange friendly links, and for convenience we usually need to do some extraction so that the links become simple and easy to use. That calls for the PHP functions ereg_replace() and eregi_replace(); the former is case sensitive, the latter is not. Format ereg_replace(regular expression pattern, substring to be replaced, target string) Splitting strings with regular expressions In most shopping site development we need arrays to combine and output customer information and product information for different pages, which requires us to use arrays. In PHP the split() and spliti() functions can do it, splitting different pieces of information apart and calling them repeatedly, improving the site’s efficiency; the former is case sensitive, the latter is not. Format split(regular expression, target string)