String functionsStrings
These functions all manipulate strings in various ways. Some more
specialized sections can be found in the regular expression and
URL handling sections.
For information on how strings behave, especially with regard to
usage of single quotes, double quotes, and escape sequences, see
the Strings entry in
the Types section of the
manual.
For even more powerful string handling and manipulating functions
take a look at the POSIX regular
expression functions and the Perl
compatible regular expression functions
addcslashesQuote string with slashes in a C styleDescriptionstringaddcslashesstringstrstringcharlist
Returns a string with backslashes before characters that are
listed in charlist parameter. It escapes
\n, \r etc. in C-like
style, characters with ASCII code lower than 32 and higher than
126 are converted to octal representation.
Be careful if you choose to escape characters 0, a, b, f, n, r,
t and v. They will be converted to \0, \a, \b, \f, \n, \r, \t
and \v.
In PHP \0 (&null;), \r (carriage return), \n (newline) and \t (tab)
are predefined escape sequences, while in C all of these are
predefined escape sequences.
charlist like "\0..\37", which would
escape all characters with ASCII code between 0 and 31.
addcslashes example
When you define a sequence of characters in the charlist argument
make sure that you know what characters come between the
characters that you set as the start and end of the range.
Also, if the first character in a range has a lower ASCII value
than the second character in the range, no range will be
constructed. Only the start, end and period characters will be
escaped. Use the ord function to find the
ASCII value for a character.
See also stripcslashes,
stripslashes,
htmlspecialchars, and
quotemeta.
addslashesQuote string with slashesDescriptionstringaddslashesstringstr
Returns a string with backslashes before characters that need
to be quoted in database queries etc. These characters are
single quote ('), double quote
("), backslash (\)
and NUL (the &null; byte).
magic_quotes_gpc is ON by default.
See also stripslashes,
htmlspecialchars, and
quotemeta.
bin2hex
Convert binary data into hexadecimal representation
Descriptionstringbin2hexstringstr
Returns an ASCII string containing the hexadecimal representation
of str. The conversion is done byte-wise
with the high-nibble first.
See also pack and unpack.
chopAlias of rtrimDescription
This function is an alias of rtrim.
chop is different than the Perl
chop() function, which removes the last
character in the string.
chrReturn a specific characterDescriptionstringchrintascii
Returns a one-character string containing the character specified
by ascii.
chr example
You can find an ASCII-table over here: &url.asciitable;.
This function complements ord. See also
sprintf with a format string of
%c.
chunk_splitSplit a string into smaller chunksDescriptionstringchunk_splitstringbodyintchunklenstringend
Can be used to split a string into smaller chunks which is useful for
e.g. converting base64_encode output to match RFC
2045 semantics. It inserts end (defaults to
"\r\n") every chunklen characters (defaults to
76). It returns the new string leaving the original string untouched.
chunk_split example
See also explode, split
and wordwrap.
convert_cyr_string
Convert from one Cyrillic character set to another
Descriptionstringconvert_cyr_stringstringstrstringfromstringto
This function returns the given string converted from one
Cyrillic character set to another. The from
and to arguments are single characters that
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
count_chars
Return information about characters used in a string
Descriptionmixedcount_charsstringstringint
mode
Counts the number of occurrences of every byte-value (0..255) in
string and returns it in various ways.
The optional parameter Mode default to
0. Depending on modecount_chars returns one of the following:
0 - an array with the byte-value as key and the frequency of
every byte as value.
1 - same as 0 but only byte-values with a frequency greater
than zero are listed.
2 - same as 0 but only byte-values with a frequency equal to
zero are listed.
3 - a string containing all used byte-values is returned.
4 - a string containing all not used byte-values is returned.
crc32Calculates the crc32 polynomial of a stringDescriptionintcrc32stringstr
Generates the cyclic redundancy checksum polynomial of 32-bit
lengths of the str. This is usually used
to validate the integrity of data being transmitted.
See also: md5cryptOne-way string encryption (hashing)Descriptionstringcryptstringstrstringsaltcrypt will return an encrypted string using the
standard Unix DES-based encryption algorithm or
alternative algorithms that may be available on the system. Arguments
are a string to be encrypted and an optional salt string to base the
encryption on. See the Unix man page for your crypt function for more
information.
If the salt argument is not provided, one will be randomly
generated by PHP.
Some operating systems support more than one type of encryption. In
fact, sometimes the standard DES-based encryption is replaced by an
MD5-based encryption algorithm. The encryption type is triggered by the
salt argument. At install time, PHP determines the capabilities of the
crypt function and will accept salts for other encryption types. If no
salt is provided, PHP will auto-generate a standard two character salt by
default, unless the default encryption type on the system is MD5, in
which case a random MD5-compatible salt is generated. PHP sets a
constant named CRYPT_SALT_LENGTH which tells you whether a regular two
character salt applies to your system or the longer twelve character salt
is applicable.
If you are using the supplied salt, you should be aware that the salt is
generated once. If you are calling this function recursively, this may
impact both appearance and security.
The standard DES-based encryption crypt returns the
salt as the first two characters of the output. It also only uses the
first eight characters of str, so longer strings
that start with the same eight characters will generate the same result
(when the same salt is used).
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:
CRYPT_STD_DES - Standard DES-based encryption with a two character salt
CRYPT_EXT_DES - Extended DES-based encryption with a nine character salt
CRYPT_MD5 - MD5 encryption with a twelve character salt starting with
$1$
CRYPT_BLOWFISH - Blowfish encryption with a sixteen character salt
starting with $2$
There is no decrypt function, since crypt
uses a one-way algorithm.
crypt examples
]]>
See also md5 and the
Mcrypt extension.
echoOutput one or more stringsDescriptionechostringarg1stringargn...
Outputs all parameters.
echo is not actually a function (it is a
language construct) so you are not required to use parentheses
with it. In fact, if you want to pass more than one parameter
to echo, you must not enclose the parameters within parentheses.
It is not possible to use echo in a
variable function
context, but you can use print instead.
echo examples
]]>
echo also has a shortcut syntax, where you
can immediately follow the opening tag with an equals sign.
foo.
]]>
This short syntax only works with the
short_open_tag configuration
setting enabled.
See also:
print,
printf, and
flush.
explodeSplit a string by stringDescriptionarrayexplodestringseparatorstringstringintlimit
Returns an array of strings, each of which is a substring of
string formed by splitting it on
boundaries formed by the string separator.
If limit is set, the returned array will
contain a maximum of limit elements with
the last element containing the rest of
string.
If separator is an empty string (""),
explode will return &false;. If
separator contains a value that is not contained
in string, then explode will
return an array containing string.
The limit parameter was added in PHP
4.0.1
explode examples
Although implode can for historical reasons
accept its parameters in either order,
explode cannot. You must ensure that the
separator argument comes before the
string argument.
See also
preg_split,
spliti,
split, and
implode.
get_html_translation_table
Returns the translation table used by
htmlspecialchars and
htmlentitiesDescriptionstringget_html_translation_tableinttableintquote_styleget_html_translation_table will return the
translation table that is used internally for
htmlspecialchars and
htmlentities. There are two new defines
(HTML_ENTITIES,
HTML_SPECIALCHARS) that allow you to
specify the table you want. And as in the
htmlspecialchars and
htmlentities functions you can optionally
specify the quote_style you are working with. The default is
ENT_COMPAT mode. See the description of these modes in
htmlspecialchars.
Translation Table Example
& Krämer";
$encoded = strtr($str, $trans);
]]>
The $encoded variable will now contain:
"Hallo & <Frau> &
Krämer".
The cool thing is using array_flip to change
the direction of the translation.
The content of $original would be: "Hallo &
<Frau> & Krämer".
See also: htmlspecialchars,
htmlentities, strtr,
and array_flip.
get_meta_tags
Extracts all meta tag content attributes from a file and returns
an array
Descriptionarrayget_meta_tagsstringfilenameintuse_include_path
Opens filename and parses it line by line
for <meta> tags of the form
Meta Tags Example
]]>
(pay attention to line endings - PHP uses a native function to
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.
Setting use_include_path to 1 will result
in PHP trying to open the file along the standard include path.
hebrev
Convert logical Hebrew text to visual text
Descriptionstringhebrevstringhebrew_textintmax_chars_per_line
The optional parameter max_chars_per_line
indicates maximum number of characters per line will be
output. The function tries to avoid breaking words.
See also hebrevchebrevc
Convert logical Hebrew text to visual text with newline conversion
Descriptionstringhebrevcstringhebrew_textintmax_chars_per_line
This function is similar to hebrev with the
difference that it converts newlines (\n) to "<br>\n". The
optional parameter max_chars_per_line
indicates maximum number of characters per line will be
output. The function tries to avoid breaking words.
See also hebrevhtmlentities
Convert all applicable characters to HTML entities
Descriptionstringhtmlentitiesstringstringintquote_stylestringcharset
This function is identical to
htmlspecialchars in all ways, except that
all characters which have HTML character entity equivalents are
translated into these entities. Like
htmlspecialchars, it takes an optional
second argument which indicates what should be done with single
and double quotes. ENT_COMPAT (the default)
will only convert double-quotes and leave single-quotes alone.
ENT_QUOTES will convert both double and
single quotes, and ENT_NOQUOTES will leave
both double and single quotes unconverted.
At present, the ISO-8859-1 character set is used as default.
Support for the optional second argument was added in PHP 3.0.17 and PHP
4.0.3.
Like htmlspecialchars, it takes an optional
third argument which defines character set used in conversion.
Support for this argument was added in PHP 4.1.0.
See also htmlspecialchars and
nl2br.
htmlspecialchars
Convert special characters to HTML entities
Descriptionstringhtmlspecialcharsstringstringintquote_stylestringcharset
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 some of these
conversions made; the translations made are those most
useful for everyday web programming. If you require all HTML
character entities to be translated, use
htmlentities instead.
This function is useful in preventing user-supplied text from
containing HTML markup, such as in a message board or guest book
application. The optional second argument, quote_style, tells
the function what to do with single and double quote characters.
The default mode, ENT_COMPAT, is the backwards compatible mode
which only translates the double-quote character and leaves the
single-quote untranslated. If ENT_QUOTES is set, both single and
double quotes are translated and if ENT_NOQUOTES is set neither
single nor double quotes are translated.
The translations performed are:
'&' (ampersand) becomes '&'
'"' (double quote) becomes '"' when ENT_NOQUOTES
is not set.
''' (single quote) becomes ''' only when
ENT_QUOTES is set.
'<' (less than) becomes '<'
'>' (greater than) becomes '>'
htmlspecialchars example
Test", ENT_QUOTES);
]]>
Note that this function does not translate anything beyond what
is listed above. For full entity translation, see
htmlentities. Support for the optional
second argument was added in PHP 3.0.17 and PHP 4.0.3.
The third argument defines character set used in conversion. The
default character set is ISO-8859-1. Support for this third argument was
added in PHP 4.1.0.
See also htmlentities and
nl2br.
implodeJoin array elements with a stringDescriptionstringimplodestringgluearraypieces
Returns a string containing a string representation of all the
array elements in the same order, with the glue string between
each element.
implode exampleimplode can, for historical reasons, accept
its parameters in either order. For consistency with
explode, however, it may be less confusing
to use the documented order of arguments.
See also explode, join,
and split.
joinJoin array elements with a stringDescriptionstringjoinstringgluearraypiecesjoin is an alias to
implode, and is identical in every way.
See also explode, implode,
and split.
levenshtein
Calculate Levenshtein distance between two strings
Descriptionintlevenshteinstringstr1stringstr2intlevenshteinstringstr1stringstr2intcost_insintcost_repintcost_delintlevenshteinstringstr1stringstr2functioncost
This function returns the Levenshtein-Distance between the
two argument strings or -1, if one of the argument strings
is longer than the limit of 255 characters (255 should be
more than enough for name or dictionary comparison, and
nobody serious would be doing genetic analysis with PHP).
The Levenshtein distance is defined as the minimal number of
characters you have to replace, insert or delete to transform
str1 into str2.
The complexity of the algorithm is O(m*n),
where n and m are the
length of str1 and
str2 (rather good when compared to
similar_text, which is O(max(n,m)**3),
but still expensive).
In its simplest form the function will take only the two
strings as parameter and will calculate just the number of
insert, replace and delete operations needed to transform
str1 into str2.
A second variant will take three additional parameters that
define the cost of insert, replace and delete operations. This
is more general and adaptive than variant one, but not as
efficient.
The third variant (which is not implemented yet) will be the most
general and adaptive, but also the slowest alternative. It will
call a user-supplied function that will determine the cost for
every possible operation.
The user-supplied function will be called with the following
arguments:
operation to apply: 'I', 'R' or 'D'
actual character in string 1
actual character in string 2
position in string 1
position in string 2
remaining characters in string 1
remaining characters in string 2
The user-supplied function has to return a positive integer
describing the cost for this particular operation, but it may
decide to use only some of the supplied arguments.
The user-supplied function approach offers the possibility to
take into account the relevance of and/or difference between
certain symbols (characters) or even the context those symbols
appear in to determine the cost of insert, replace and delete
operations, but at the cost of losing all optimizations done
regarding cpu register utilization and cache misses that have
been worked into the other two variants.
See also soundex,
similar_text, and
metaphone.
localeconvGet numeric formatting informationDescriptionarraylocaleconv
Returns an associative array containing localized numeric and
monetary formatting information.
localeconv returns data based upon the current locale
as set by setlocale. The associative array that is
returned contains the following fields:
Array elementDescriptiondecimal_pointDecimal point characterthousands_sepThousands separatorgroupingArray containing numeric groupingsint_curr_symbolInternational currency symbol (i.e. USD)currency_symbolLocal currency symbol (i.e. $)mon_decimal_pointMonetary decimal point charactermon_thousands_sepMonetary thousands separatormon_groupingArray containing monetary groupingspositive_signSign for positive valuesnegative_signSign for negative valuesint_frac_digitsInternational fractional digitsfrac_digitsLocal fractional digitsp_cs_precedes
&true; if currency_symbol precedes a positive value, &false;
if it succeeds one
p_sep_by_space
&true; if a space separates currency_symbol from a positive
value, &false; otherwise
n_cs_precedes
&true; if currency_symbol precedes a negative value, &false;
if it succeeds one
n_sep_by_space
&true; if a space separates currency_symbol from a negative
value, &false; otherwise
p_sign_posn0
Parentheses surround the quantity and currency_symbol1
The sign string precedes the quantity and currency_symbol
2
The sign string succeeds the quantity and currency_symbol
3
The sign string immediately precedes the currency_symbol
4
The sign string immediately succeeds the currency_symbol
n_sign_posn0
Parentheses surround the quantity and currency_symbol
1
The sign string precedes the quantity and currency_symbol
2
The sign string succeeds the quantity and currency_symbol
3
The sign string immediately precedes the currency_symbol
4The sign string immediately succeeds the currency_symbol
The grouping fields contain arrays that define the way numbers
should be grouped. For example, the grouping field for the en_US
locale, would contain a 2 item array with the values 3 and 3.
The higher the index in the array, the farther left the grouping
is. If an array element is equal to CHAR_MAX, no further
grouping is done. If an array element is equal to 0, the
previous element should be used.
localeconv example
\n";
echo "--------------------------------------------\n";
echo " Monetary information for current locale: \n";
echo "--------------------------------------------\n\n";
echo "int_curr_symbol: {$locale_info["int_curr_symbol"]}\n";
echo "currency_symbol: {$locale_info["currency_symbol"]}\n";
echo "mon_decimal_point: {$locale_info["mon_decimal_point"]}\n";
echo "mon_thousands_sep: {$locale_info["mon_thousands_sep"]}\n";
echo "positive_sign: {$locale_info["positive_sign"]}\n";
echo "negative_sign: {$locale_info["negative_sign"]}\n";
echo "int_frac_digits: {$locale_info["int_frac_digits"]}\n";
echo "frac_digits: {$locale_info["frac_digits"]}\n";
echo "p_cs_precedes: {$locale_info["p_cs_precedes"]}\n";
echo "p_sep_by_space: {$locale_info["p_sep_by_space"]}\n";
echo "n_cs_precedes: {$locale_info["n_cs_precedes"]}\n";
echo "n_sep_by_space: {$locale_info["n_sep_by_space"]}\n";
echo "p_sign_posn: {$locale_info["p_sign_posn"]}\n";
echo "n_sign_posn: {$locale_info["n_sign_posn"]}\n";
echo "\n";
]]>
The constant CHAR_MAX is also defined for the use mentioned above.
See also: setlocale.
ltrim
Strip whitespace from the beginning of a string
Descriptionstringltrimstringstrstringcharlist
The second parameter was added in PHP 4.1.0
This function returns a string with whitespace stripped from the
beginning of str.
Without the second parameter,
ltrim will strip these characters:
" " (ASCII 32
(0x20)), an ordinary space.
"\t" (ASCII 9
(0x09)), a tab.
"\n" (ASCII 10
(0x0A)), a new line (line feed).
"\r" (ASCII 13
(0x0D)), a carriage return.
"\0" (ASCII 0
(0x00)), the NUL-byte.
"\x0B" (ASCII 11
(0x0B)), a vertical tab.
You can also specify the characters you want to strip, by means
of the charlist parameter.
Simply list all characters that you want to be stripped. With
.. you can specify a range of characters.
Usage example of ltrim
]]>
See also trim and rtrim.
md5Calculate the md5 hash of a stringDescriptionstringmd5stringstr
Calculates the MD5 hash of str using the
RSA Data Security, Inc.
MD5 Message-Digest Algorithm, and returns that hash.
The hash is a 32-character hexadecimal number.
See also: crc32 and md5_filemd5_fileCalculates the md5 hash of a given filenameDescriptionstringmd5_filestringfilename
Calculates the MD5 hash of the specified
filename using the
RSA Data Security, Inc.
MD5 Message-Digest Algorithm, and returns that hash.
This function has the same purpose of the command line utility
md5sum.
See also: md5 and crc32metaphoneCalculate the metaphone key of a stringDescriptionstringmetaphonestringstr
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.
Metaphone was developed by Lawrence Philips
<lphilips@verity.com>. It is described in ["Practical
Algorithms for Programmers", Binstock & Rex, Addison Wesley,
1995].
nl_langinfo
Query language and locale information
Descriptionstringnl_langinfointitem
&warn.undocumented.func;
nl2br
Inserts HTML line breaks before all newlines in a string
Descriptionstringnl2brstringstring
Returns string with '<br />' inserted
before all newlines.
Starting with PHP 4.0.5, nl2br is now XHTML
compliant. All versions before 4.0.5 will return
string with '<br>' inserted before
newlines instead of '<br />'.
See also htmlspecialchars,
htmlentities and
wordwrap.
ordReturn ASCII value of characterDescriptionintordstringstring
Returns the ASCII value of the first character of
string. This function complements
chr.
ord example
You can find an ASCII-table over here: &url.asciitable;.
See also chr.
parse_strParses the string into variablesDescriptionvoidparse_strstringstrarrayarr
Parses str as if it were the query string
passed via an URL and sets variables in the current scope. If
the second parameter arr is present,
variables are stored in this variable as an array elements instead.
Support for the optional second parameter was added in PHP 4.0.3.
Using parse_str
See also set_magic_quotes_runtime
and urldecode.
printOutput a stringDescriptionprintstringarg
Outputs arg. &return.success;
print is not actually a real function (it is a
language construct) so you are not required to use parentheses with it.
This also means that you can not use print (just
like echo) as a variable function.
print examples
]]>
See also echo, printf,
and flush.
printfOutput a formatted stringDescriptionvoidprintfstringformatmixedargs
Produces output according to format, which
is described in the documentation for sprintf.
See also: print, sprintf,
sscanf, fscanf,
and flush.
quoted_printable_decode
Convert a quoted-printable string to an 8 bit string
Descriptionstringquoted_printable_decodestringstr
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.
quotemetaQuote meta charactersDescriptionstringquotemetastringstr
Returns a version of str with a backslash character
(\) before every character that is among
these: . \\ + * ? [ ^ ] ( $ )
See also addslashes,
htmlentities,
htmlspecialchars,
nl2br, and
stripslashes.
str_rot13Perform the rot13 transform on a stringDescriptionstringstr_rot13stringstr
This function performs the ROT13 encoding on the
str argument and returns the resulting
string. The ROT13 encoding simply shifts every letter by 13
places in the alphabet while leaving non-alpha characters
untouched. Encoding and decoding are done by the same function,
passing an encoded string as argument will return the original version.
rtrim
Strip whitespace from the end of a string
Descriptionstringrtrimstringstrstringcharlist
The second parameter was added in PHP 4.1.0
This function returns a string with whitespace stripped from the
end of str.
Without the second parameter,
rtrim will strip these characters:
" " (ASCII 32
(0x20)), an ordinary space.
"\t" (ASCII 9
(0x09)), a tab.
"\n" (ASCII 10
(0x0A)), a new line (line feed).
"\r" (ASCII 13
(0x0D)), a carriage return.
"\0" (ASCII 0
(0x00)), the NUL-byte.
"\x0B" (ASCII 11
(0x0B)), a vertical tab.
You can also specify the characters you want to strip, by means
of the charlist parameter.
Simply list all characters that you want to be stripped. With
.. you can specify a range of characters.
Usage example of rtrim
]]>
See also trim and ltrim.
sscanf
Parses input from a string according to a format
Descriptionmixedsscanfstringstrstringformatstringvar1
The function sscanf is the input analog of
printf. sscanf reads
from the string str and interprets it
according to the specified format. If only
two parameters were passed to this function, the values parsed
will be returned as an array.
sscanf Example
If optional parameters are passed, the function will return the
number of assigned values. The optional parameters must be passed
by reference.
sscanf - using optional parameters$first$last
\n";
]]>
See also: fscanf, printf,
and sprintf.
setlocaleSet locale informationDescriptionstringsetlocalemixedcategorystringlocaleCategory is a named constant (or string)
specifying the category of the functions affected by the locale
setting:
LC_ALL for all of the below
LC_COLLATE for string comparison, see
strcoll
LC_CTYPE for character classification and conversion, for
example strtoupper
LC_MONETARY for localeconv LC_NUMERIC for decimal separator (See also:
localeconv)
LC_TIME for date and time formatting with
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".
If locale is zero or "0", the locale setting
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 platform, the specified
locale does not exist or the category name is invalid.
An invalid category name also causes a warning message.
setlocale Examples
]]>
similar_text
Calculate the similarity between two strings
Descriptionintsimilar_textstringfirststringsecondfloatpercent
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.
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.
soundexCalculate the soundex key of a stringDescriptionstringsoundexstringstr
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.
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.
Soundex Examples
See also levenshtein,
metaphone, and similar_text.
sprintfReturn a formatted stringDescriptionstringsprintfstringformatmixedargs
Returns a string produced according to the formatting string
format.
The format string is composed of 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.
Each conversion specification consists of a percent sign
(%), followed by one or more of these
elements, in order:
An optional padding specifier that says
what character will be used for padding the results to the
right string size. This may be a space character or a
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.
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.
An optional number, a width specifier
that says how many characters (minimum) this conversion should
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
float. (Another function useful for formatting numbers is
number_format.)
A type specifier that says what type the
argument data should be treated as. Possible types:
% - a literal percent character. No
argument is required.
b - the argument is treated as an
integer, and presented as a binary number.
c - the argument is treated as an
integer, and presented as the character with that ASCII
value.
d - the argument is treated as an
integer, and presented as a (signed) decimal number.
u - the argument is treated as an
integer, and presented as an unsigned decimal number.
f - the argument is treated as a
float, and presented as a floating-point number.
o - the argument is treated as an
integer, and presented as an octal number.
s - the argument is treated as and
presented as a string.
x - the argument is treated as an integer
and presented as a hexadecimal number (with lowercase
letters).
X - the argument is treated as an integer
and presented as a hexadecimal number (with uppercase
letters).
As of PHP version 4.0.6 the format string supports argument
numbering/swapping. Here is an example:
Argument swapping
This might output, "There are 5 monkeys in the tree". But
imagine we are creating a format string in a separate file,
commonly because we would like to internationalize it and we
rewrite it as:
Argument swapping
We now have a problem. The order of the placeholders in the
format string does not match the order of the arguments in the
code. We would like to leave the code as is and simply indicate
in the format string which arguments the placeholders refer to.
We would write the format string like this instead:
Argument swapping
An added benefit here is that you can repeat the placeholders without
adding more arguments in the code. For example:
Argument swapping
See also: printf,
sscanf, fscanf, and
number_format.
Examplessprintf: zero-padded integerssprintf: formatting currencystrncasecmp
Binary safe case-insensitive string comparison of the first n
characters
Descriptionintstrncasecmpstringstr1stringstr2intlen
This function is similar to strcasecmp, with
the difference that you can specify the (upper limit of the)
number of characters (len) from each
string to be used in the comparison. If any of the strings is
shorter than len, then the length of that
string will be used for the comparison.
Returns < 0 if str1 is less than
str2; > 0 if str1
is greater than str2, and 0 if they are
equal.
See also ereg, strcasecmp,
strcmp, substr,
stristr, and strstr.
strcasecmp
Binary safe case-insensitive string comparison
Descriptionintstrcasecmpstringstr1stringstr2
Returns < 0 if str1 is less than
str2; > 0 if str1
is greater than str2, and 0 if they are
equal.
strcasecmp example
See also ereg, strcmp,
substr, stristr,
strncasecmp, and
strstr.
strchr
Find the first occurrence of a character
Descriptionstringstrchrstringhaystackstringneedle
This function is an alias for strstr, and is
identical in every way.
strcmpBinary safe string comparisonDescriptionintstrcmpstringstr1stringstr2
Returns < 0 if str1 is less than
str2; > 0 if str1
is greater than str2, and 0 if they are
equal.
Note that this comparison is case sensitive.
See also ereg,
strcasecmp, substr,
stristr, strncasecmp,
strncmp, and strstr.
strcollLocale based string comparisonDescriptionintstrcollstringstr1stringstr2
Returns < 0 if str1 is less than
str2; > 0 if
str1 is greater than
str2, and 0 if they are equal.
strcoll uses the current locale for doing
the comparisons. If the current locale is C or POSIX, this
function is equivalent to strcmp.
Note that this comparison is case sensitive, and unlike
strcmp this function is not binary safe.
See also ereg, strcmp,
strcasecmp, substr,
stristr, strncasecmp,
strncmp, strstr, and
setlocale.
strcspn
Find length of initial segment not matching mask
Descriptionintstrcspnstringstr1stringstr2
Returns the length of the initial segment of
str1 which does not
contain any of the characters in str2.
See also strspn.
strip_tagsStrip HTML and PHP tags from a stringDescriptionstringstrip_tagsstringstrstringallowable_tags
This function tries to return a string with all HTML and PHP tags
stripped from a given str. 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.
You can use the optional second parameter to specify tags which
should not be stripped.
allowable_tags was added in PHP 3.0.13
and PHP 4.0b3.
strip_tags example');
]]>
This function does not modify any attributes on the tags that you allow
using allowable_tags, including the
style and onmouseover attributes
that a mischievous user may abuse when posting text that will be shown
to other users.
stripcslashes
Un-quote string quoted with addcslashesDescriptionstringstripcslashesstringstr
Returns a string with backslashes stripped off. Recognizes
C-like \n, \r ..., octal
and hexadecimal representation.
See also addcslashes.
stripslashes
Un-quote string quoted with addslashesDescriptionstringstripslashesstringstr
Returns a string with backslashes stripped off.
(\' becomes ' and so on.)
Double backslashes are made into a single backslash.
See also addslashes.
stristr
Case-insensitive strstrDescriptionstringstristrstringhaystackstringneedle
Returns all of haystack from the first
occurrence of needle to the end.
needle and haystack
are examined in a case-insensitive manner.
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.
See also strchr,
strrchr, substr, and
ereg.
strlenGet string lengthDescriptionintstrlenstringstr
Returns the length of string.
strnatcmp
String comparisons using a "natural order" algorithm
Descriptionintstrnatcmpstringstr1stringstr2
This function implements a comparison algorithm that orders
alphanumeric strings in the way a human being would, this is
described as a "natural ordering". An example of the difference
between this algorithm and the regular computer string sorting
algorithms (used in strcmp) can be seen
below:
The code above will generate the following output:
img1.png
[1] => img10.png
[2] => img12.png
[3] => img2.png
)
Natural order string comparison
Array
(
[0] => img1.png
[1] => img2.png
[2] => img10.png
[3] => img12.png
)
]]>
For more information see: Martin Pool's Natural Order String Comparison
page.
Similar to other string comparison functions, this one returns
< 0 if str1 is less than
str2; > 0 if
str1 is greater than
str2, and 0 if they are equal.
Note that this comparison is case sensitive.
See also ereg,
strcasecmp, substr,
stristr, strcmp,
strncmp, strncasecmp,
strnatcasecmp, strstr,
natsort and natcasesort.
strnatcasecmp
Case insensitive string comparisons using a "natural order"
algorithm
Descriptionintstrnatcasecmpstringstr1stringstr2
This function implements a comparison algorithm that orders
alphanumeric strings in the way a human being would. The
behaviour of this function is similar to
strnatcmp, except that the comparison is not
case sensitive. For more infomation see: Martin Pool's Natural Order String Comparison
page.
Similar to other string comparison functions, this one returns
< 0 if str1 is less than
str2; > 0 if
str1 is greater than
str2, and 0 if they are equal.
See also ereg,
strcasecmp, substr,
stristr, strcmp,
strncmp, strncasecmp,
strnatcmp, and strstr.
strncmp
Binary safe string comparison of the first n characters
Descriptionintstrncmpstringstr1stringstr2intlen
This function is similar to strcmp, with the
difference that you can specify the (upper limit of the) number
of characters (len) from each string to be
used in the comparison. If any of the strings is shorter than
len, then the length of that string will
be used for the comparison.
Returns < 0 if str1 is less than
str2; > 0 if str1
is greater than str2, and 0 if they are
equal.
Note that this comparison is case sensitive.
See also ereg,
strncasecmp,
strcasecmp, substr,
stristr, strcmp, and
strstr.
str_pad
Pad a string to a certain length with another string
Descriptionstringstr_padstringinputintpad_lengthstringpad_stringintpad_type
This functions returns the input string
padded on the left, the right, or both sides to the specified
padding length. If the optional argument
pad_string is not supplied, the
input is padded with spaces, otherwise it
is padded with characters from pad_string
up to the limit.
Optional argument pad_type can be
STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. If
pad_type is not specified it is assumed to
be STR_PAD_RIGHT.
If the value of pad_length is negative or
less than the length of the input string, no padding takes place.
str_pad examplestrpos
Find position of first occurrence of a string
Descriptionintstrposstringhaystackstringneedleintoffset
Returns the numeric position of the first occurrence of
needle in the
haystack string. Unlike the
strrpos, this function can take a full
string as the needle parameter and the
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:
If needle is not a string, it is converted
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.
See also strrpos,
strrchr, substr,
stristr, and strstr.
strrchr
Find the last occurrence of a character in a string
Descriptionstringstrrchrstringhaystackstringneedle
This function returns the portion of
haystack which starts at the last
occurrence of needle and goes until the
end of haystack.
Returns &false; if needle is not found.
If needle contains more than one
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.
strrchr example
See also strchr, substr,
stristr, and strstr.
str_repeatRepeat a stringDescriptionstringstr_repeatstringinputintmultiplier
Returns input_str repeated
multiplier times.
multiplier has to be greater than 0.
str_repeat example
This will output "-=-=-=-=-=-=-=-=-=-=".
See also substr_count.
strrevReverse a stringDescriptionstringstrrevstringstring
Returns string, reversed.
Reversing a string with strrev
]]>
strrpos
Find position of last occurrence of a char in a string
Descriptionintstrrposstringhaystackcharneedle
Returns the numeric position of the last occurrence of
needle in the
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.
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:
If needle is not a string, it is converted
to an integer and applied as the ordinal value of a character.
See also strpos,
strrchr, substr,
stristr, and strstr.
strspn
Find length of initial segment matching mask
Descriptionintstrspnstringstr1stringstr2
Returns the length of the initial segment of
str1 which consists entirely of characters
in str2.
The line of code:
will assign 2 to $var, because the string "42" will
be the longest segment containing characters from "1234567890".
See also strcspn.
strstrFind first occurrence of a stringDescriptionstringstrstrstringhaystackstringneedle
Returns part of haystack string from the
first occurrence of needle to the end of
haystack.
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.
This function is case-sensitive. For case-insensitive searches, use
stristr.
strstr example
See also ereg, preg_match,
strchr, stristr,
strpos, strrchr, and
substr.
strtokTokenize stringDescriptionstringstrtokstringarg1stringarg2strtok splits a string (arg1)
into smaller strings (tokens), with each token being delimited by any
character from arg2.
That is, if you have a string like "This is an example string" you
could tokenize this string into its individual words by using the
space character as the token.
strtok example
";
$tok = strtok(" \n\t");
}
]]>
Note that only the first call to strtok uses the string argument.
Every subsequent call to strtok only needs the token to use, as
it keeps track of where it is in the current string. To start
over, or to tokenize a new string you simply call strtok with the
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.
The behavior when an empty part was found changed with PHP 4.1.0. The old
behavior returned an empty string, while the new, correct, behavior
simply skips the part of the string:
Old strtok behaviorNew strtok behavior
Also be careful that your tokens may be equal to "0". This
evaluates to &false; in conditional expressions.
See also split and
explode.
strtolowerMake a string lowercaseDescriptionstringstrtolowerstringstr
Returns string with all alphabetic
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.
strtolower example
See also strtoupper,
ucfirst,
and ucwords.
strtoupperMake a string uppercaseDescriptionstringstrtoupperstringstring
Returns string with all alphabetic
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.
strtoupper example
See also strtolower,
ucfirst,
and ucwords.
str_replace
Replace all occurrences of the search string with the replacement string
Descriptionmixedstr_replacemixedsearchmixedreplacemixedsubject
This function returns a string or an array with all occurences of
search in subject
replaced with the given replace value. If
you don't need fancy replacing rules, you should always use this
function instead of ereg_replace or
preg_replace.
In PHP 4.0.5 and later, every parameter to
str_replace can be an array.
If subject is an array, then the search
and replace is performed with every entry of
subject, and the return value is an array
as well.
If search and
replace are arrays, then
str_replace takes a value from each array
and uses them to do search and replace on
subject. If
replace has fewer values than
search, then an empty string is used for
the rest of replacement values. If search
is an array and replace is a string; then
this replacement string is used for every value of
search.
str_replace example
");
]]>
This function is binary safe.
str_replace was added in PHP 3.0.6, but was
buggy up until PHP 3.0.8.
See also ereg_replace,
preg_replace, and strtr.
strtrTranslate certain charactersDescriptionstringstrtrstringstrstringfromstringtostringstrtrstringstrarrayreplace_pairs
This function returns a copy of str,
translating all occurrences of each character in
from to the corresponding character in
to and returning the result.
If from and to are
different lengths, the extra characters in the longer of the two
are ignored.
strtr examplestrtr can be called with only two
arguments. If called with two arguments it behaves in a new way:
from then has to be an array that contains
string -> string pairs that will be replaced in the source
string. strtr will always look for the
longest possible match first and will *NOT* try to replace stuff
that it has already worked on.
Examples:
"hi", "hi" => "hello");
echo strtr("hi all, I said hello", $trans) . "\n";
]]>
This will show: "hello all, I said hi",
This optional to and
from parameters were added in PHP 4.0.0
See also ereg_replace.
substrReturn part of a stringDescriptionstringsubstrstringstringintstartintlength
Substr returns the portion of string
specified by the start and
length parameters.
If start is positive, the returned string
will start at the start'th position in
string, counting from zero. For instance,
in the string 'abcdef', the character at
position 0 is 'a', the
character at position 2 is
'c', and so forth.
Basic substr usage
If start is negative, the returned string
will start at the start'th character
from the end of string.Using a negative start
If length is given and is positive, the string
returned will contain at most length characters
beginning from start (depending on the length of
string. If string is less
than start characters long, &false; will be
returned.
If length is given and is negative, then that many
characters will be omitted from the end of string
(after the start position has been calculated when a
start is negative). If
start denotes a position beyond this truncation,
an empty string will be returned.
Using a negative length
See also strrchr and
ereg.
substr_countCount the number of substring occurrencesDescriptionintsubstr_countstringhaystackstringneedlesubstr_count returns the number of times the
needle substring occurs in the
haystack string.
substr_count examplesubstr_replaceReplace text within a portion of a stringDescriptionstringsubstr_replacestringstringstringreplacementintstartintlengthsubstr_replace replaces a copy of
string delimited by the
start and (optionally)
length parameters with the string given in
replacement. The result is returned.
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
string which is to be replaced. If it is
negative, it represents the number of characters from the end of
string at which to stop replacing. If it
is not given, then it will default to strlen(
string ); i.e. end the replacing at the
end of string.
substr_replace example
\n";
/* These two examples replace all of $var with 'bob'. */
echo substr_replace($var, 'bob', 0) . " \n";
echo substr_replace($var, 'bob', 0, strlen($var)) . " \n";
/* Insert 'bob' right at the beginning of $var. */
echo substr_replace($var, 'bob', 0, 0) . " \n";
/* These next two replace 'MNRPQR' in $var with 'bob'. */
echo substr_replace($var, 'bob', 10, -1) . " \n";
echo substr_replace($var, 'bob', -7, -1) . " \n";
/* Delete 'MNRPQR' from $var. */
echo substr_replace($var, '', 10, -1) . " \n";
?>
]]>
See also str_replace and
substr.
trim
Strip whitespace from the beginning and end of a string
Descriptionstringtrimstringstrstringcharlist
The optional charlist parameter was
added in PHP 4.1.0
This function returns a string with whitespace stripped from the
beginning and end of str.
Without the second parameter,
trim will strip these characters:
" " (ASCII 32
(0x20)), an ordinary space.
"\t" (ASCII 9
(0x09)), a tab.
"\n" (ASCII 10
(0x0A)), a new line (line feed).
"\r" (ASCII 13
(0x0D)), a carriage return.
"\0" (ASCII 0
(0x00)), the NUL-byte.
"\x0B" (ASCII 11
(0x0B)), a vertical tab.
You can also specify the characters you want to strip, by means
of the charlist parameter.
Simply list all characters that you want to be stripped. With
.. you can specify a range of characters.
Usage example of trim
]]>
See also ltrim and rtrim.
ucfirstMake a string's first character uppercaseDescriptionstringucfirststringstr
Returns a string with the first character of
str capitalized, if 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.
ucfirst example
See also strtolower, strtoupper,
and ucwords.
ucwords
Uppercase the first character of each word in a string
Descriptionstringucwordsstringstr
Returns a string with the first character of each word in
str capitalized, if that character is
alphabetic.
ucwords example
The definition of a word is any string of characters
that is immediately after a whitespace (These are:
space, form-feed, newline, carriage return, horizontal tab,
and vertical tab).
See also strtoupper,
strtolower and ucfirst.
vprintfOutput a formatted stringDescriptionvoidvprintfstringformatarrayargs
Display array values as a formatted string according to
format (which is described in the documentation
for sprintf).
Operates as printf but accepts an array of
arguments, rather than a variable number of arguments.
See also: printf, sprintf,
vsprintfvsprintfReturn a formatted stringDescriptionstringvsprintfstringformatarrayargs
Return array values as a formatted string according to
format (which is described in the documentation
for sprintf).
Operates as sprintf but accepts an array of
arguments, rather than a variable number of arguments.
See also: sprintf, vsprintf,
vprintfwordwrap
Wraps a string to a given number of characters using a string
break character.
Descriptionstringwordwrapstringstrintwidthstringbreakintcut
Returns a string with str wrapped
at the column number specified by the (optional)
width parameter. The line is broken
using the (optional) break parameter.
wordwrap will automatically wrap at column
75 and break using '\n' (newline) if width
or break are not given.
If the cut is set to 1, the string is
always wrapped at the specified width. So if you have a word
that is larger than the given width, it is broken apart.
(See second example).
The optional cut parameter was added in PHP 4.0.3
wordwrap example
This example would display:
wordwrap example
This example would display:
See also nl2br.