diff --git a/functions/strings.xml b/functions/strings.xml index c6355d2b97..cea548a64d 100644 --- a/functions/strings.xml +++ b/functions/strings.xml @@ -1,17 +1,19 @@ String functions Strings + These functions all manipulate strings in various ways. Some more specialized sections can be found in the regular expression and - URL handling sections. + URL handling sections. + AddCSlashes - Quote string with slashes in a C style + Quote string with slashes in a C style. Description @@ -30,23 +32,23 @@ charlist like "\0..\37", which would escape all characters with ASCII code between 0 and 31. - addcslashes() example - -$escaped = addcslashes($not_escaped, "\0..\37!@\177..\377"); - - + <function>Addcslashes</function> example + +$escaped = addcslashes ($not_escaped, "\0..\37!@\177..\377"); + + Added in PHP4b3-dev. - - + + See also stripcslashes, stripslashes, htmlspecialchars, htmlspecialchars, and - quotemeta. - + quotemeta. + @@ -66,13 +68,13 @@ $escaped = addcslashes($not_escaped, "\0..\37!@\177..\377"); to be quoted in database queries etc. These characters are single quote ('), double quote ("), backslash (\) - and NUL (the null byte). - + and NUL (the null byte). + See also stripslashes, htmlspecialchars, and - quotemeta. - + quotemeta. + @@ -92,15 +94,15 @@ $escaped = addcslashes($not_escaped, "\0..\37!@\177..\377"); Returns an ASCII string containing the hexadecimal representation of str. The conversion is done byte-wise - with the high-nibble first. - + with the high-nibble first. + Chop - remove trailing whitespace + Remove trailing whitespace. Description @@ -109,22 +111,25 @@ $escaped = addcslashes($not_escaped, "\0..\37!@\177..\377"); string str - Returns the argument string without trailing whitespace, including newlines. + Returns the argument string without trailing whitespace, + including newlines. - chop() example - -$trimmed = Chop($line); - - - See also trim. - + <function>Chop</function> example + +$trimmed = chop ($line); + + + + + See also trim. + Chr - Return a specific character + Return a specific character. Description @@ -136,59 +141,63 @@ $trimmed = Chop($line); Returns a one-character string containing the character specified by ascii. - <function>chr</function> example - -$str .= chr(27); /* add an escape character at the end of $str */ + <function>Chr</function> example + +$str .= chr (27); /* add an escape character at the end of $str */ /* Often this is more useful */ -$str = sprintf("The string ends in escape: %c", 27); - +$str = sprintf ("The string ends in escape: %c", 27); + + This function complements ord. See also sprintf with a format string of - %c. - + %c. + chunk_split - Split a string into smaller chunks + Split a string into smaller chunks. Description string chunk_split string string - int chunklen - - string end - + int + chunklen + + string + end + Can be used to split a string into smaller chunks which is useful - for e.g. converting base64_encode output to + for e.g. converting base64_encode output to match RFC 2045 semantics. It inserts every chunklen (defaults to 76) chars the string end (defaults to "\r\n"). It returns the new string leaving the original string untouched. - <function>chunk_split</function> example - + <function>Chunk_split</function> example + # format $data using RFC 2045 semantics -$new_string = chunk_split(base64_encode($data)); - - - +$new_string = chunk_split (base64_encode($data)); + + This function is significantly faster than ereg_replace. - - - - This function was added in 3.0.6. - + + + This function was added in 3.0.6. + + + @@ -196,7 +205,7 @@ $new_string = chunk_split(base64_encode($data)); convert_cyr_string - Convert from one Cyrillic character set to another + Convert from one Cyrillic character set to another. @@ -214,19 +223,38 @@ $new_string = chunk_split(base64_encode($data)); represent the source and target Cyrillic character sets. The supported types are: - - k - koi8-r - - w - windows-1251 - - i - iso8859-5 - - a - x-cp866 - - d - x-cp866 - - m - x-mac-cyrillic - + + + k - koi8-r + + + + + w - windows-1251 + + + + + i - iso8859-5 + + + + + a - x-cp866 + + + + + d - x-cp866 + + + + + m - x-mac-cyrillic + + + + @@ -234,7 +262,7 @@ $new_string = chunk_split(base64_encode($data)); count_chars - Return information abouts characters used in a string + Return information abouts characters used in a string. @@ -296,7 +324,7 @@ $new_string = chunk_split(base64_encode($data)); crypt - DES-encrypt a string + DES-encrypt a string. Description @@ -307,16 +335,17 @@ $new_string = chunk_split(base64_encode($data)); salt - crypt will encrypt a string using the standard Unix DES encryption method. Arguments are a string to be encrypted and an optional two-character salt string to base the encryption on. See the Unix man page for your - crypt function for more information. + crypt function for more information. + If the salt argument is not provided, it will be randomly - generated by PHP. + generated by PHP. + Some operating systems support more than one type of encryption. In fact, sometimes the standard DES encryption is replaced by an @@ -329,14 +358,17 @@ $new_string = chunk_split(base64_encode($data)); random MD5-compatible salt is generated. PHP sets a constant named CRYPT_SALT_LENGTH which tells you whether a regular 2-character salt applies to your system or the longer 12-char MD5 - salt is applicable. + salt is applicable. + The standard DES encryption crypt contains - the salt as the first two characters of the output. + the salt as the first two characters of the output. + On systems where the crypt() function supports multiple encryption types, the following constants are set to 0 or 1 - depending on whether the given type is available: + depending on whether the given type is available: + @@ -361,17 +393,17 @@ $new_string = chunk_split(base64_encode($data)); - There is no decrypt function, since crypt - uses a one-way algorithm. + uses a one-way algorithm. + echo - Output one or more strings + Output one or more strings. Description @@ -383,15 +415,15 @@ $new_string = chunk_split(base64_encode($data)); - Outputs all parameters. - + Outputs all parameters. + - echo is not actually a function (it - is a language construct) so you are not required to use - parantheses with it. - - echo example - + Echo is not actually a function (it is a + language construct) so you are not required to use parantheses + with it. + + <function>Echo</function> example + echo "Hello World"; echo "This spans @@ -399,23 +431,21 @@ multiple lines. The newlines will be output as well"; echo "This spans\nmultiple lines. The newlines will be\noutput as well."; - + - - + In fact, if you want to pass more than one parameter to echo, you must not enclose the parameters within parentheses. - See also: print, printf, and - flush. - + flush. + @@ -432,28 +462,27 @@ echo "This spans\nmultiple lines. The newlines will be\noutput as well."; string string - Returns an array of strings containing the elements separated by separator. - - <function>explode</function> example - + <function>Explode</function> example + $pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; -$pieces = explode(" ", $pizza); - - +$pieces = explode (" ", $pizza); + + + See also split and - implode. - + implode. + flush - Flush the output buffer + Flush the output buffer. Description @@ -464,8 +493,8 @@ $pieces = explode(" ", $pizza); Flushes the output buffers of PHP and whatever backend PHP is using (CGI, a web server, etc.) This effectively tries to push - all the output so far to the user's browser. - + all the output so far to the user's browser. + @@ -475,7 +504,7 @@ $pieces = explode(" ", $pizza); Returns the translation table used by htmlspecialchars and - htmlentities + htmlentities. @@ -494,7 +523,6 @@ $pieces = explode(" ", $pizza); (HTML_ENTITIES, HTML_SPECIALCHARS) that allow you to specify the table you want. - Translation Table Example @@ -503,7 +531,6 @@ $str = "Hallo & <Frau> & Kr $encoded = strtr ($str, $trans); - The $encoded variable will now contain: "Hallo &amp; &lt;Frau&gt; @@ -539,7 +566,7 @@ $original = strtr ($str, $trans); get_meta_tags Extracts all meta tag content attributes from a file and returns - an array + an array. @@ -554,28 +581,29 @@ $original = strtr ($str, $trans); Opens filename and parses it line by line for <meta> tags of the form - - - Meta Tags Example - + + Meta Tags Example + <meta name="author" content="name"> <meta name="tags" content="php3 documentation"> </head> <!-- parsing stops here --> - - + + (pay attention to line endings - PHP uses a native function to - parse the input, so a Mac file won't work on Unix). - + parse the input, so a Mac file won't work on Unix). + The value of the name property becomes the key, the value of the content property becomes the value of the returned array, so you can easily use standard array functions to traverse it or access single values. Special characters in the value of the name property are substituted with '_', the rest is converted to lower - case. - + case. + + Setting use_include_path to 1 will result - in PHP trying to open the file along the standard include path. + in PHP trying to open the file along the standard include path. + @@ -583,7 +611,7 @@ $original = strtr ($str, $trans); htmlentities - Convert all applicable characters to HTML entities + Convert all applicable characters to HTML entities. @@ -594,15 +622,17 @@ $original = strtr ($str, $trans); This function is identical to - htmlspecialchars in all ways, except that + Htmlspecialchars in all ways, except that all characters which have HTML entity equivalents are translated - into these entities. + into these entities. + - At present, the ISO-8859-1 character set is used. + At present, the ISO-8859-1 character set is used. + See also htmlspecialchars and - nl2br. - + nl2br. + @@ -610,7 +640,7 @@ $original = strtr ($str, $trans); htmlspecialchars - Convert special characters to HTML entities + Convert special characters to HTML entities. @@ -623,37 +653,47 @@ $original = strtr ($str, $trans); Certain characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. This function returns a string with these conversions - made. + made. + This function is useful in preventing user-supplied text from containing HTML markup, such as in a message board or guest book - application. + application. + At present, the translations that are done are: - '&' (ampersand) becomes '&amp;' + '&' (ampersand) becomes '&amp;' + + - '"' (double quote) becomes '&quot;' + '"' (double quote) becomes '&quot;' + + - '<' (less than) becomes '&lt;' + '<' (less than) becomes '&lt;' + + - '>' (greater than) becomes '&gt;' - - + '>' (greater than) becomes '&gt;' + + + + Note that this functions does not translate anything beyond what is listed above. For full entity translation, see - htmlentities. - + htmlentities. + See also htmlentities and - nl2br. - + nl2br. + @@ -674,25 +714,23 @@ $original = strtr ($str, $trans); array elements in the same order, with the glue string between each element. - implode() example - -$colon_separated = implode(":", $array); - - - - + <function>Implode</function> example + +$colon_separated = implode (":", $array); + + + See also explode, join, and split. - - + join - Join array elements with a string + Join array elements with a string. Description @@ -701,17 +739,14 @@ $colon_separated = implode(":", $array); string glue array pieces - join is an alias to implode, and is identical in every way. - - + See also explode, implode, and split. - - + @@ -719,7 +754,7 @@ $colon_separated = implode(":", $array); ltrim - Strip whitespace from the beginning of a string + Strip whitespace from the beginning of a string. @@ -728,20 +763,20 @@ $colon_separated = implode(":", $array); string ltrim string str - This function strips whitespace from the start of a string and - returns the stripped string. + returns the stripped string. + - See also chop and trim. - + See also chop and trim. + md5 - Calculate the md5 hash of a string + Calculate the md5 hash of a string. Description @@ -749,19 +784,18 @@ $colon_separated = implode(":", $array); string md5 string str - Calculates the MD5 hash of str using the RSA Data - Security, Inc. MD5 Message-Digest Algorithm. - + Security, Inc. MD5 Message-Digest Algorithm. + Metaphone - Calculate the metaphone key of a string + Calculate the metaphone key of a string. Description @@ -770,26 +804,29 @@ $colon_separated = implode(":", $array); string str - Calculates the metaphone key of str. + Calculates the metaphone key of str. + Similar to soundex metaphone creates the same key for similar sounding words. It's more accurate than soundex as it knows the basic rules of - English pronunciation. the metaphone generated keys are of - variable length. + English pronunciation. The metaphone generated keys are of + variable length. + Metaphone was developed by Lawrence Philips <lphilips@verity.com>. It is described in ["Practical Algorithms for Programmers", Binstock & Rex, Addison Wesley, 1995]. - - - This function was added in PHP 4.0. - + + + This function was added in PHP 4.0. + + + - nl2br @@ -801,22 +838,21 @@ $colon_separated = implode(":", $array); string nl2br string string - Returns string with '<BR>' inserted - before all newlines. - + before all newlines. + See also htmlspecialchars and - htmlentities. - + htmlentities. + Ord - Return ASCII value of character + Return ASCII value of character. Description @@ -829,23 +865,24 @@ $colon_separated = implode(":", $array); string. This function complements chr. - <function>ord</function> example - -if (ord($str) == 10) { + <function>Ord</function> example + +if (ord ($str) == 10) { echo "The first character of \$str is a line feed.\n"; } - - + + + - See also chr. - + See also chr. + parse_str - Parses the string into variables + Parses the string into variables. Description @@ -853,22 +890,22 @@ if (ord($str) == 10) { void parse_str string str - Parses str as if it were the query string - passed via an URL and sets variables in the current scope. - + passed via an URL and sets variables in the current scope. + Using <function>parse_str</function> - + $str = "first=value&second[]=this+works&second[]=another"; parse_str($str); -echo $first; /* prints "value" */ +echo $first; /* prints "value" */ echo $second[0]; /* prints "this works" */ echo $second[1]; /* prints "another" */ - + + @@ -884,21 +921,19 @@ echo $second[1]; /* prints "another" */ string arg - Outputs arg. - + Outputs arg. + - See also: - echo - printf - flush - + See also: echo, printf, + and flush. + printf - output a formatted string + Output a formatted string. Description @@ -909,24 +944,23 @@ echo $second[1]; /* prints "another" */ args... - Produces output according to format, which - is described in the documentation for sprintf. - + is described in the documentation for sprintf. + - See also: - print, - sprintf, and - flush. - + See also: print, + sprintf, and flush. + quoted_printable_decode - Convert a quoted-printable string to an 8 bit string + + Convert a quoted-printable string to an 8 bit string. + Description @@ -940,7 +974,8 @@ echo $second[1]; /* prints "another" */ This function returns an 8-bit binary string corresponding to the decoded quoted printable string. This function is similar to imap_qprint, except this one does not - require the IMAP module to work. + require the IMAP module to work. + @@ -958,15 +993,15 @@ echo $second[1]; /* prints "another" */ Returns a version of str with a backslash character (\) before every character that is among - these: . \\ + * ? [ ^ ] ( $ ) - + these: . \\ + * ? [ ^ ] ( $ ) + See also addslashes, htmlentities, htmlspecialchars, nl2br, and - stripslashes. - + stripslashes. + @@ -986,18 +1021,18 @@ echo $second[1]; /* prints "another" */ (%) signs followed by two hex digits have been replaced with literal characters. For example, the string foo%20bar%40baz decodes into foo - bar@baz - + bar@baz. + - See also rawurlencode. - + See also rawurlencode. + rawurlencode - URL-encode according to RFC1738 + URL-encode according to RFC1738. Description @@ -1015,31 +1050,31 @@ echo $second[1]; /* prints "another" */ with character conversions (like some email systems). For example, if you want to include a password in an ftp url: - <function>rawurlencode</function> example 1 - + <function>Rawurlencode</function> example 1 + echo '<A HREF="ftp://user:', rawurlencode ('foo @+%/'), '@ftp.my.com/x.txt">'; - - + + Or, if you pass information in a path info component of the url: - - <function>rawurlencode</function> example 2 - + <function>Rawurlencode</function> example 2 + echo '<A HREF="http://x.com/department_list_script/', - rawurlencode ('sales and marketing/Miami'), '">'; - - + rawurlencode ('sales and marketing/Miami'), '">'; + + + - See also rawurldecode. - + See also rawurldecode. + setlocale - Set locale information + Set locale information. Description @@ -1049,7 +1084,7 @@ echo '<A HREF="http://x.com/department_list_script/', string locale - category is a string specifying the + Category is a string specifying the category of the functions affected by the locale setting: @@ -1085,31 +1120,33 @@ echo '<A HREF="http://x.com/department_list_script/', strftime - - + + If locale is the empty string "", the locale names will be set from the values of environment variables with the same names as the above - categories, or from "LANG". - + categories, or from "LANG". + If locale is zero or "0", the locale setting - is not affected, only the current setting is returned. - + is not affected, only the current setting is returned. + Setlocale returns the new current locale, or false if the locale functionality is not implemented in the plattform, the specified locale does not exist or the category name is invalid. - An invalid category name also causes a warning message. - + An invalid category name also causes a warning message. + similar_text - Calculate the similarity between two strings + + Calculate the similarity between two strings. + Description @@ -1121,20 +1158,20 @@ echo '<A HREF="http://x.com/department_list_script/', percent - This calculates the similarity between two strings as described in Oliver [1993]. Note that this implementation does not use a stack as in Oliver's pseudo code, but recursive calls which may or may not speed up the whole process. Note also that the complexity of this algorithm is O(N**3) where N is the length of - the longest string. - + the longest string. + By passing a reference as third argument, similar_text will calculate the similarity in percent for you. It returns the number of matching chars in - both strings. + both strings. + @@ -1149,30 +1186,33 @@ echo '<A HREF="http://x.com/department_list_script/', string soundex string str - - Calculates the soundex key of str. + Calculates the soundex key of str. + Soundex keys have the property that words pronounced similarly produce the same soundex key, and can thus be used to simplify searches in databases where you know the pronunciation but not the spelling. This soundex function returns a string 4 characters - long, starting with a letter. + long, starting with a letter. + This particular soundex function is one described by Donald Knuth in "The Art Of Computer Programming, vol. 3: Sorting And - Searching", Addison-Wesley (1973), pp. 391-392. + Searching", Addison-Wesley (1973), pp. 391-392. + Soundex Examples - -soundex("Euler") == soundex("Ellery") == 'E460'; -soundex("Gauss") == soundex("Ghosh") == 'G200'; -soundex("Knuth") == soundex("Kant") == 'H416'; -soundex("Lloyd") == soundex("Ladd") == 'L300'; -soundex("Lukasiewicz") == soundex("Lissajous") == 'L222'; + +soundex ("Euler") == soundex ("Ellery") == 'E460'; +soundex ("Gauss") == soundex ("Ghosh") == 'G200'; +soundex ("Knuth") == soundex ("Kant") == 'H416'; +soundex ("Lloyd") == soundex ("Ladd") == 'L300'; +soundex ("Lukasiewicz") == soundex ("Lissajous") == 'L222'; - + + @@ -1190,19 +1230,18 @@ soundex("Lukasiewicz") == soundex("Lissajous") == 'L222'; args... - Returns a string produced according to the formatting string - format. - + format. + The format string is composed by zero or more directives: ordinary characters (excluding %) that are copied directly to the result, and conversion specifications, each of which results in fetching its own parameter. This applies to both sprintf - and printf. - + and printf. + Each conversion specification consists of these elements, in order: @@ -1215,30 +1254,37 @@ soundex("Lukasiewicz") == soundex("Lissajous") == 'L222'; 0 (zero character). The default is to pad with spaces. An alternate padding character can be specified by prefixing it with a single quote ('). - See the examples below. + See the examples below. + + An optional alignment specifier that says if the result should be left-justified or right-justified. The default is right-justified; a - - character here will make it left-justified. + character here will make it left-justified. + + An optional number, a width specifier that says how many characters (minimum) this conversion should - result in. + result in. + + An optional precision specifier that says how many decimal digits should be displayed for floating-point numbers. This option has no effect for other types than double. (Another function useful for formatting numbers is - number_format.) + number_format.) + + A type specifier that says what type the argument data should be treated as. Possible types: - % - a literal percent character. No @@ -1279,35 +1325,36 @@ soundex("Lukasiewicz") == soundex("Lissajous") == 'L222'; and presented as a hexadecimal number (with uppercase letters). - - - + + + + + See also: printf and - number_format. - + number_format. + - Examples - sprintf: zero-padded integers - -$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day); - - + <function>Sprintf</function>: zero-padded integers + +$isodate = sprintf ("%04d-%02d-%02d", $year, $month, $day); + + - sprintf: formatting currency - + <function>Sprintf</function>: formatting currency + $money1 = 68.75; $money2 = 54.35; $money = $money1 + $money2; // echo $money will output "123.1"; $formatted = sprintf ("%01.2f", $money); // echo $formatted will output "123.10" - - + + @@ -1316,7 +1363,7 @@ $formatted = sprintf ("%01.2f", $money); strcasecmp - Binary safe case-insensitive string comparison + Binary safe case-insensitive string comparison. @@ -1330,13 +1377,13 @@ $formatted = sprintf ("%01.2f", $money); Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are - equal. - + equal. + See also ereg, strcmp, substr, stristr, and - strstr. - + strstr. + @@ -1354,11 +1401,10 @@ $formatted = sprintf ("%01.2f", $money); string haystack string needle - This function is an alias for strstr, and is - identical in every way. - + identical in every way. + @@ -1378,16 +1424,16 @@ $formatted = sprintf ("%01.2f", $money); Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are - equal. - + equal. + - Note that this comparison is case sensitive. - + Note that this comparison is case sensitive. + See also ereg, strcasecmp, substr, - stristr, and strstr. - + stristr, and strstr. + @@ -1395,7 +1441,7 @@ $formatted = sprintf ("%01.2f", $money); strcspn - Find length of initial segment not matching mask + Find length of initial segment not matching mask. @@ -1408,11 +1454,11 @@ $formatted = sprintf ("%01.2f", $money); Returns the length of the initial segment of str1 which does not - contain any of the characters in str2. - + contain any of the characters in str2. + - See also strspn. - + See also strspn. + @@ -1426,21 +1472,26 @@ $formatted = sprintf ("%01.2f", $money); string strip_tags string str - string allowable_tags + string + allowable_tags + This function tries to strip all HTML and PHP tags from the given string. It errors on the side of caution in case of incomplete or bogus tags. It uses the same tag stripping state machine as - the fgetss function. + the fgetss function. + You can use the optional second parameter to specify tags which should not be stripped. - - - - allowable_tags was added in PHP 3.0.13, PHP4B3. - + + + Allowable_tags was added in PHP 3.0.13, + PHP4B3. + + + @@ -1459,14 +1510,15 @@ $formatted = sprintf ("%01.2f", $money); Returns a string with backslashes stripped off. Recognizes C-like \n, \r ..., octal and hexadecimal representation. - - Added in PHP4b3-dev. - + Added in PHP4b3-dev. + + + - See also addcslashes. - + See also addcslashes. + @@ -1484,11 +1536,11 @@ $formatted = sprintf ("%01.2f", $money); Returns a string with backslashes stripped off. (\' becomes ' and so on.) - Double backslashes are made into a single backslash. - + Double backslashes are made into a single backslash. + - See also addslashes. - + See also addslashes. + @@ -1496,7 +1548,7 @@ $formatted = sprintf ("%01.2f", $money); stristr - Case-insensitive strstr + Case-insensitive strstr. @@ -1510,23 +1562,27 @@ $formatted = sprintf ("%01.2f", $money); Returns all of haystack from the first occurrence of needle to the end. needle and haystack - are examined in a case-insensitive manner. + are examined in a case-insensitive manner. + - If needle is not found, returns false. + If needle is not found, returns false. + If needle is not a string, it is converted - to an integer and applied as the ordinal value of a character. + to an integer and applied as the ordinal value of a character. + See also strchr, strrchr, substr, and - ereg. - - + ereg. + + + strlen - Get string length + Get string length. Description @@ -1535,7 +1591,8 @@ $formatted = sprintf ("%01.2f", $money); string str - Returns the length of string. + Returns the length of string. + @@ -1543,7 +1600,7 @@ $formatted = sprintf ("%01.2f", $money); strpos - Find position of first occurrence of a string + Find position of first occurrence of a string. @@ -1562,47 +1619,56 @@ $formatted = sprintf ("%01.2f", $money); haystack string. Unlike the strrpos, this function can take a full string as the needle parameter and the - entire string will be used. + entire string will be used. + If needle is not found, returns false. - - It is easy to mistake the return values for "character found - at position 0" and "character not found". Here's how to detect the - difference: - + + + It is easy to mistake the return values for "character found at + position 0" and "character not found". Here's how to detect + the difference: + + // in PHP 4.0b3 and newer: -$pos = strpos("b", $mystring); +$pos = strpos ("b", $mystring); if ($pos === false) { // note: three equal signs // not found... } // in versions older than 4.0b3: -$pos = strpos("b", $mystring); -if (is_string($pos) && !$pos) { +$pos = strpos ("b", $mystring); +if (is_string ($pos) && !$pos) { // not found... } - + + + + If needle is not a string, it is converted - to an integer and applied as the ordinal value of a character. + to an integer and applied as the ordinal value of a character. + The optional offset parameter allows you to specify which character in haystack to start searching. The position returned is still relative to the - the beginning of haystack. + the beginning of haystack. + See also strrpos, strrchr, substr, - stristr, and strstr. - - + stristr, and strstr. + + + strrchr - Find the last occurrence of a character in a string + Find the last occurrence of a character in a string. @@ -1616,39 +1682,41 @@ if (is_string($pos) && !$pos) { This function returns the portion of haystack which starts at the last occurrence of needle and goes until the - end of haystack. + end of haystack. + - Returns false if needle is not found. + Returns false if needle is not found. + If needle contains more than one - character, the first is used. + character, the first is used. + If needle is not a string, it is converted to an integer and applied as the ordinal value of a character. - - <function>strrchr</function> example - + <function>Strrchr</function> example + // get last directory in $PATH -$dir = substr( strrchr( $PATH, ":" ), 1 ); +$dir = substr (strrchr ($PATH, ":"), 1); // get everything after last newline $text = "Line 1\nLine 2\nLine 3"; -$last = substr( strrchr( $text, 10 ), 1 ); +$last = substr (strrchr ($text, 10), 1 ); - - + + See also substr, - stristr, and strstr. - + stristr, and strstr. + str_repeat - Repeat a string + Repeat a string. Description @@ -1663,27 +1731,26 @@ $last = substr( strrchr( $text, 10 ), 1 ); multiplier has to be greater than 0. - <function>str_repeat</function> example - -echo str_repeat("-=", 10); + <function>Str_repeat</function> example + +echo str_repeat ("-=", 10); This will output "-=-=-=-=-=-=-=-=-=-=". - - This function was added in PHP 4.0. + This function was added in PHP 4.0. + - strrev - Reverse a string + Reverse a string. Description @@ -1692,7 +1759,8 @@ echo str_repeat("-=", 10); string string - Returns string, reversed. + Returns string, reversed. + @@ -1700,7 +1768,7 @@ echo str_repeat("-=", 10); strrpos - Find position of last occurrence of a char in a string + Find position of last occurrence of a char in a string. @@ -1716,24 +1784,28 @@ echo str_repeat("-=", 10); haystack string. Note that the needle in this case can only be a single character. If a string is passed as the needle, then only the first character of that string will - be used. + be used. + - If needle is not found, returns false. + If needle is not found, returns false. + If needle is not a string, it is converted - to an integer and applied as the ordinal value of a character. + to an integer and applied as the ordinal value of a character. + See also strpos, strrchr, substr, - stristr, and strstr. - - + stristr, and strstr. + + + strspn - Find length of initial segment matching mask + Find length of initial segment matching mask. @@ -1746,10 +1818,12 @@ echo str_repeat("-=", 10); Returns the length of the initial segment of str1 which consists entirely of characters - in str2. + in str2. + - See also strcspn. - + See also strcspn. + + @@ -1766,25 +1840,28 @@ echo str_repeat("-=", 10); Returns all of haystack from the first - occurrence of needle to the end. + occurrence of needle to the end. + - If needle is not found, returns false. + If needle is not found, returns false. + If needle is not a string, it is converted to an integer and applied as the ordinal value of a character. - - <function>strstr</function> example - - $email = 'sterling@designmultimedia.com'; - $domain = strstr($email, '@'); - print $domain; // prints designmultimedia.com + + <function>Strstr</function> example + +$email = 'sterling@designmultimedia.com'; +$domain = strstr ($email, '@'); +print $domain; // prints designmultimedia.com See also stristr, strrchr, substr, and - ereg. + ereg. + @@ -1806,16 +1883,17 @@ echo str_repeat("-=", 10); could tokenize this string into its individual words by using the space character as the token. - <function>strtok</function> example - - $string = "This is an example string"; - $tok = strtok($string," "); - while($tok) { - echo "Word=$tok<br>"; - $tok = strtok(" "); - } + <function>Strtok</function> example + +$string = "This is an example string"; +$tok = strtok ($string," "); +while ($tok) { + echo "Word=$tok<br>"; + $tok = strtok (" "); +} - + + Note that only the first call to strtok uses the string argument. Every subsequent call to strtok only needs the token to use, as @@ -1824,20 +1902,23 @@ echo str_repeat("-=", 10); string argument again to initialize it. Note that you may put multiple tokens in the token parameter. The string will be tokenized when any one of the characters in the argument are - found. + found. + Also be careful that your tokens may be equal to "0". This - evaluates to false in conditional expressions. + evaluates to false in conditional expressions. + See also split and - explode. + explode. + strtolower - Make a string lowercase + Make a string lowercase. Description @@ -1847,29 +1928,32 @@ echo str_repeat("-=", 10); Returns string with all alphabetic - characters converted to lowercase. + characters converted to lowercase. + Note that 'alphabetic' is determined by the current locale. This means that in i.e. the default "C" locale, characters such as - umlaut-A (Ä) will not be converted. + umlaut-A (Ä) will not be converted. + - <function>strtolower</function> example - - $str = "Mary Had A Little Lamb and She LOVED It So"; - $str = strtolower($str); - print $str; # Prints mary had a little lamb and she loved it so - + <function>Strtolower</function> example + +$str = "Mary Had A Little Lamb and She LOVED It So"; +$str = strtolower($str); +print $str; # Prints mary had a little lamb and she loved it so + + See also strtoupper - and ucfirst. - + and ucfirst. + strtoupper - Make a string uppercase + Make a string uppercase. Description @@ -1879,21 +1963,25 @@ echo str_repeat("-=", 10); Returns string with all alphabetic - characters converted to uppercase. + characters converted to uppercase. + Note that 'alphabetic' is determined by the current locale. For instance, in the default "C" locale characters such as umlaut-a - (ä) will not be converted. + (ä) will not be converted. + - <function>strtoupper</function> example - - $str = "Mary Had A Little Lamb and She LOVED It So"; - $str = strtoupper($str); - print $str; # Prints MARY HAD A LITTLE LAMB AND SHE LOVED IT SO - + <function>Strtoupper</function> example + +$str = "Mary Had A Little Lamb and She LOVED It So"; +$str = strtoupper ($str); +print $str; # Prints MARY HAD A LITTLE LAMB AND SHE LOVED IT SO + + See also strtolower - and ucfirst. + and ucfirst. + @@ -1901,7 +1989,7 @@ echo str_repeat("-=", 10); str_replace - Replace all occurrences of needle in haystack with str + Replace all occurrences of needle in haystack with str. @@ -1920,31 +2008,32 @@ echo str_repeat("-=", 10); instead of ereg_replace. - str_replace() example - -$bodytag = str_replace("%body%", "black", "<body text=%body%>"); + <function>Str_replace</function> example + +$bodytag = str_replace ("%body%", "black", "<body text=%body%>"); - + + This function is binary safe. - - - - str_replace was added in PHP 3.0.6, but was - buggy up until PHP 3.0.8. - - + + + + Str_replace was added in PHP 3.0.6, but was + buggy up until PHP 3.0.8. + + See also ereg_replace and strtr. - + strtr - Translate certain characters + Translate certain characters. Description @@ -1958,17 +2047,19 @@ $bodytag = str_replace("%body%", "black", "<body text=%body%>"); This function operates on str, translating all occurrences of each character in from to the corresponding character in to and - returning the result. + returning the result. + If from and to are different lengths, the extra characters in the longer of the two are ignored. - strtr() example - + <function>Strtr</function> example + $addr = strtr($addr, "äåö", "aao"); - + + strtr can be called with only two arguments. If called with two arguments it behaves in a new way: @@ -1986,7 +2077,7 @@ $trans = array ("hello" => "hi", "hi" => "hello"); echo strtr("hi all, I said hello", $trans) . "\n"; - This will show: "hello all, I said hi", + This will show: "hello all, I said hi", @@ -2017,19 +2108,22 @@ echo strtr("hi all, I said hello", $trans) . "\n"; Substr returns the portion of string specified by the start and - length parameters. + length parameters. + If start is positive, the returned string will start at the start'th character of - string. + string. + Examples: -$rest = substr("abcdef", 1); // returns "bcdef" -$rest = substr("abcdef", 1, 3); // returns "bcd" +$rest = substr ("abcdef", 1); // returns "bcdef" +$rest = substr ("abcdef", 1, 3); // returns "bcd" - + + If start is negative, the returned string will start at the start'th character @@ -2038,74 +2132,76 @@ $rest = substr("abcdef", 1, 3); // returns "bcd" Examples: -$rest = substr("abcdef", -1); // returns "f" -$rest = substr("abcdef", -2); // returns "ef" -$rest = substr("abcdef", -3, 1); // returns "d" +$rest = substr ("abcdef", -1); // returns "f" +$rest = substr ("abcdef", -2); // returns "ef" +$rest = substr ("abcdef", -3, 1); // returns "d" - + + If length is given and is positive, the string returned will end length characters from start. If this would result in a string with negative length (because the start is past the end of the string), then the returned string will contain the single - character at start. + character at start. + If length is given and is negative, the string returned will end length characters from the end of string. If this would result in a string with negative length, then the returned string will contain the single character at - start. + start. + Examples: -$rest = substr("abcdef", 1, -1); // returns "bcde" +$rest = substr ("abcdef", 1, -1); // returns "bcde" - + + See also strrchr and - ereg. + ereg. + - - substr_replace - Replace text within a portion of a string. + substr_replace + Replace text within a portion of a string. - Description - - string substr_replace - string string - string replacement - int start - int length - - - - substr_replace replaces the part of - string delimited by the - start and (optionally) - length parameters with the string given in - replacement. The result is returned. - - + Description + + string substr_replace + string string + string replacement + int start + int + length + + - If start is positive, the replacing will - begin at the start'th offset into - string. - - + substr_replace replaces the part of + string delimited by the + start and (optionally) + length parameters with the string given in + replacement. The result is returned. + - If start is negative, the replacing will - begin at the start'th character from - the end of string. - - + If start is positive, the replacing will + begin at the start'th offset into + string. + + + If start is negative, the replacing will + begin at the start'th character from the + end of string. + If length is given and is positive, it represents the length of the portion of @@ -2115,54 +2211,49 @@ $rest = substr("abcdef", 1, -1); // returns "bcde" is not given, then it will default to strlen( string ); i.e. end the replacing at the end of string. - - - - - <function>substr_replace</function> example - + + + + <function>Substr_replace</function> example + <?php $var = 'ABCDEFGH:/MNRPQR/'; echo "Original: $var<hr>\n"; /* These two examples replace all of $var with 'bob'. */ -echo substr_replace( $var, 'bob', 0 ) . "<br>\n"; -echo substr_replace( $var, 'bob', 0, strlen( $var ) ) . "<br>\n"; +echo substr_replace ($var, 'bob', 0) . "<br>\n"; +echo substr_replace ($var, 'bob', 0, strlen ($var)) . "<br>\n"; /* Insert 'bob' right at the beginning of $var. */ -echo substr_replace( $var, 'bob', 0, 0 ) . "<br>\n"; +echo substr_replace ($var, 'bob', 0, 0) . "<br>\n"; /* These next two replace 'MNRPQR' in $var with 'bob'. */ -echo substr_replace( $var, 'bob', 10, -1 ) . "<br>\n"; -echo substr_replace( $var, 'bob', -7, -1 ) . "<br>\n"; +echo substr_replace ($var, 'bob', 10, -1) . "<br>\n"; +echo substr_replace ($var, 'bob', -7, -1) . "<br>\n"; /* Delete 'MNRPQR' from $var. */ -echo substr_replace( $var, '', 10, -1 ) . "<br>\n"; +echo substr_replace ($var, '', 10, -1) . "<br>\n"; ?> - - - - - - See also str_replace and - substr. - - - - - substr_replace was added in PHP 4.0. - - - + + + + + See also str_replace and + substr. + + + + Substr_replace was added in PHP 4.0. + + - trim - Strip whitespace from the beginning and end of a string + Strip whitespace from the beginning and end of a string. @@ -2173,11 +2264,14 @@ echo substr_replace( $var, '', 10, -1 ) . "<br>\n"; This function strips whitespace from the start and the end of a - string and returns the stripped string. The whitespace characters it currently - strips are: "\n", "\r", "\t", "\v", "\0", and " ". + string and returns the stripped string. The whitespace + characters it currently strips are: "\n", "\r", "\t", "\v", "\0", + and " ". + See also chop and - ltrim. + ltrim. + @@ -2185,7 +2279,7 @@ echo substr_replace( $var, '', 10, -1 ) . "<br>\n"; ucfirst - Make a string's first character uppercase + Make a string's first character uppercase. @@ -2196,14 +2290,17 @@ echo substr_replace( $var, '', 10, -1 ) . "<br>\n"; Capitalizes the first character of str if - that character is alphabetic. + that character is alphabetic. + Note that 'alphabetic' is determined by the current locale. For instance, in the default "C" locale characters such as umlaut-a - (ä) will not be converted. + (ä) will not be converted. + See also strtoupper and - strtolower. + strtolower. + @@ -2222,10 +2319,12 @@ echo substr_replace( $var, '', 10, -1 ) . "<br>\n"; Capitalizes the first character of each word in - str if that character is alphabetic. + str if that character is alphabetic. + See also strtoupper, - strtolower and ucfirst. + strtolower and ucfirst. +