Contents / Previous / Next


Operations on Strings

String Concatenation

The concatenation operator for strings is the point ('.'), the concatenating assignment operator is('.='). Example: $a = "Hello "; $b = $a . "World!"; // $b contains "Hello World!" $a = "Hello "; $a .= "World!"; // $a contains "Hello World!"


White Space

string chop (string str):
Returns the argument string without trailing whitespace, including newlines.

string trim (string str):
Returns a string with whitespace stripped from the beginning and end of str. The whitespace characters it currently strips are: "\n", "\r", "\t", "\v", "\0", and a plain space.
There are also rtrim() and ltrim().


ASCII <-> Char

int ord (string string):
Returns the ASCII value of the first character of string.

string chr (int ascii):
Returns a one-character string containing the character specified by ascii.


Sub-Strings

string substr (string string, int start [, int length]):
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, else from the end of string.

string substr_replace (string string, string replacement, int start [, int length]):
Replaces a copy of string delimited by the start and (optionally) length parameters with the string given in replacement. The result is returned.

int strpos (string haystack, string needle [, int offset]):
Finds position of first occurrence of a string.

int substr_count (string haystack, string needle):
Counts the number of substring occurrences.


Counts and Compare

int strlen (string str):
Returns the length of string.

mixed count_chars (string string [, mode]):
Counts the number of occurrences of every byte-value (0..255) in string and returns it in various ways, depending on mode. The default is an array with the byte-value as key and the frequency of every byte as value.

int strcmp (string str1, string str2):
Binary safe string comparison.
Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal. This comparison is case sensitive.

int levenshtein (string str1, string str2):
Calculate Levenshtein distance between two strings.
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).

int similar_text (string first, string second [, double percent]):
This calculates the similarity between two strings as described in Oliver [1993].

string soundex (string 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.
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.

string metaphone (string 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.
Metaphone was developed by Lawrence Philips . It is described in ["Practical Algorithms for Programmers", Binstock & Rex, Addison Wesley, 1995].


Transform and Crypt

string quotemeta (string str):
Quote meta characters.
Returns a version of str with a backslash character (\) before every character that is among these: . \\ + * ? [ ^ ] ( $ )

string crypt (string str [, string salt]):
Will return an encrypted 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. If the salt argument is not provided, one will be randomly generated by PHP.
There is no decrypt function, since crypt() uses a one-way algorithm.

string md5 (string str):
Calculates the md5 hash of a string. This is used for passwords.

int crc32 (string str):
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.


URL HTML Strings Function

array parse_url (string url):
Returns an associative array returning any of the various components of the URL that are present. This includes the "scheme", "host", "port", "user", "pass", "path", "query", and "fragment"

void parse_str (string str [, array arr]):
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. Example:

$str = "first=value&second[]=this+works&second[]=another"; parse_str($str); echo $first; /* prints "value" */ echo $second[0]; /* prints "this works" */ echo $second[1]; /* prints "another" */

string strip_tags (string str [, string allowable_tags]):
Strips HTML and PHP tags from a string.
You can use the optional second parameter to specify tags which should not be stripped.

string nl2br (string string):
Inserts HTML line breaks before all newlines in a string.

string htmlspecialchars (string string [, int quote_style]):
Converts special characters to HTML entities.

string htmlentities (string string [, int quote_style]):
Converts all applicable characters to HTML entities.


C like String Functions

string sprintf (string format [, mixed args...]):
Returns a string produced according to the formatting string format.

There is also also: printf(), sscanf(), fscanf(), and flush().