These functions manipulate strings in various ways. Some more specialized sections can be found in the regular expression and URL-handling sections. Many of PHP's other extensions also deal with string and character manipulation - most notably, the character type, Pspell, recode, regular expression, URL, and variable functions.
OverviewStrings are one of the most important parts of the PHP language. Most textual data are represented as strings, and a good portion of many scripts are dedicated to processing, cleaning, escaping, parsing, and transforming text.
Not surprisingly, PHP has a wide range of functions for dealing with string data.
Using the String FunctionsMany functions that find substrings or position of a substring within another string return FALSE when the specified substring is not found. This is appropriate behavior; however, due to PHP's loose typing, FALSE and 0 can appear to be the same value. Consider this code snippet:
<?php
$string = 'Jacob Two-Two Meets the Hooded Fang';
$substring = 'Jacob';
// The wrong way to do it - Jacob is found at position 0
// and the while loop exits before running the body of the loop
$pos = 0;
while ($pos = strpos($string, $substring, $pos)) {
echo "Found '$substring' at position $pos\n";
$pos += strlen($substring);
}
// A better way to do it - explicitly test for FALSE
// using the strict 'not equals' comparison operator (!==)
// Now the code will report that the substring 'Jacob' starts at offset 0
$pos = 0;
while (FALSE !== ($pos = strpos($string, $substring, $pos))) {
echo "Found '$substring' at position $pos\n";
$pos += strlen($substring);
}
?>
The moral of this story has two parts:
Configuring the String FunctionsThere are no php.ini configuration directives that directly control the behavior of the string functions; however, a few configuration directives are very closely related, as shown in the following table.
| Directive Name | Value Type | Description |
|---|---|---|
| magic_quotes_gpc | boolean (on/off) | If this directive is enabled, data received from GET/POST/Cookie sources or data parsed by parse_str() are processed automatically with the addslashes() function. |
| magic_quotes_runtime | boolean (on/off) | If this directive is enabled, data received from many functions that retrieve data from external sources (such as the database and program execution functions) are processed automatically with the addslashes() function. |
| magic_quotes_sybase | boolean (on/off) | If magic_quotes_sybase is enabled, single quotes escaped by magic_quotes_gpc or magic_quotes_runtime are escaped with a leading single quote, rather than a backslash - '' rather than \' |
The --enable-magic-quotes configure option can be set at compile time to enable magic_quotes_gpc and magic_quotes_runtime automatically.
Installing String Function SupportThese functions are built into PHP by default and can only be disabled by editing the source code and recompiling or by using the disable_functions directive in php.ini.
Table of Contents