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.
TipThe --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
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing
(Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch).
This material may be distributed only subject to the terms and conditions set forth
in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4
for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All
persons and entities shall have neither liability nor responsibility to any person or entity with respect to
any loss or damage arising from the information contained in this book.
|