String Functions

Sep 01, 2010 Author: Jenna

This section contains functions that work with strings.
Escapes single quotes, double quotes, backslashes, and \0 in strings. Format: $string_escaped = addslashes(“string”);

base64_encode, base64_decode
Encodes/decodes a string of base-64–coded characters, usually binary data.
Format: $string_encoded = base64_encode(“string”);

Truncates blank spaces at the end of a string.
Format: $chopped = chop(“string”);

Returns a single ASCII character for the number code.
Format: $char = chr(code)

Creates an associative array out of a string. Each element has a character as a key, and the value is the number of that character in the string. This function has some options for only returning characters with count 0 or with nonzero and others. Default, when no options is specified, is all characters.
Format: $array = count_chars($string,option);

Outputs a list of one or more items.
Format: echo item1,item2,item3, . . .

ereg, eregi
Searches a string for pattern. The function eregi works the same but is case-insensitive.
Format: $bool = ereg(“pattern”,$string);

ereg_replace, eregi_replace
Searches a string for pattern and replaces pattern with newchar. Ereg_replace is case-sensitive; eregi_replace is not.
Format: $newstring = ereg_replace(“pattern”,”newchars”, $string);

Creates an array. Each element is part of the string, split at sep.
Format: $array_out = explode(“sep”,$string);

Converts HTML entities to special characters in a string.
Format: $string_out = htmlentities($orig_string);

Converts special characters to HTML entities, such as & to &.
Format: $string_out($string);

Joins every element in an array into a string, separated by sep.
Format: $string_out = ($array,”sep”);

Inserts a <br /> before all new line characters (\n) in $string.
Format: $string_out = ($string);

Returns the ASCII value of the first character in the string.
Format: $integer = ord(“string”);

Creates an associative array. Each element is part of the URL.
Format: $array = parse_url($url);

Outputs item, where item can be a string, a number, or a variable.
Format: print item;

Outputs a string formatted according to format.
Format: printf(“format”,arg1,arg2,arg3, . . .);

split, spliti
Creates array. Each element is part of a specified string, split based on the regular expression pattern. Split is case-sensitive; spliti is not.
Format: $array = split(“pattern”,$string); $array = spliti(“pattern”,$string);

Returns a string formatted according to format.
Format: $string = sprintf(“format”,arg1,arg2,arg3 . . .);

Returns a string that is padded to make it number long. The character specified by pad is used to pad the string.
Format: $string_out = str_pad($string,numberh,”pad”);

Returns a string that contains $string repeated number times.
Format: $string_out = str_repeat($string,number);

Finds all instances of oldtext in $string and replaces them with newtext.
Format: $string_out = str_replace(“oldtext”,”newtext”,$string)

strchr, strrchar
The function strchr returns part of string from char to end of $string, and strrchar returns $string from char to start of string.
Format: $string_part = strchr($string,”char”);

strcmp, strcasecmp
Compares two strings on alphabetical and numerical order . Returns @@n1 if str1 is less, 0 if two strings are equal, or +1 if str1 is greater. strcmp is casesensitive; strcasecmp is not.
Format: strcasecmp($str1,$str2);

Returns the position of the first occurrence of char in $string.
Format: $int = strcspn($string,”char”);

Removes HTML and PHP tags from string. The value allowed is optional and specifies tags that should not be stripped.
Format: $string_stripped = strinp_tags($string,”allowed”);

strlen Returns the number of characters in $string.
Format: $length = strlen($string);

strpos, strrpos
strpos returns the position of the first occurrence of char in $string. strrpos returns position of last occurrence of char in string.
Format: $integer = strpos($string,”char”); $integer = strrpos($string,”char”);

Returns the length of the substring in $string that matches text.
Format: $length = strspn($string,”text”);

strstr, stristr
Returns part of $string from the first occurrence of char to end of $string. Strstr is case-sensitive; trichar is case-insensitive.
Format: $str_part = strstr($string,”char”); $str_part = strstr($string,”char”);

strtolower, strtoupper
Converts $string to lowercase or uppercase.
Format: $string_lower = strtolower($string);

Converts from characters in $string to characters in to.
Format: $string_out = strtr($string,”from”,”to”);

Returns a substring of $string. Starts at start and reads number characters.
Format: $substring = substr($string,start,number);

Replaces a substring with newtext. Starts at start and reads number characters.
Format: $string_new = substr_replace($string,”newtext”,start,number);

trim, ltrim, rtrim
Removes whitespace characters from $string. The trim function removes from beginning and end; ltrim removes from beginning; rtrim removes from end.
Format: $string_new = trim($string);

Converts first character in $string to uppercase.
Format: $string_new = ucfirst($string);

Converts first character of each word in $string to uppercase.
Format: $string_new = ucwords($string);

Inserts end-of-line character (\r\n) into $string every length characters.
Format: $string_out = wordwrap($string,length);

views 5248
  1. Add New Comment