PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




String Functions


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:
  • You need to know what the function actually returns. Some functions return 0 on error; others return 0 on success.

  • Use the strict comparison operators (such as !== and === ) to ensure that you're testing for the right return value and type.



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 \'


Tip

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.




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.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.