String Functions

Sep 01, 2010 Author: Jenna

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

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

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

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

count_chars
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);

echo
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);

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

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

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

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

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

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

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

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

printf
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);

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

str_pad
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”);

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

str_replace
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);

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

strip_tags
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”);

strspn
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);

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

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

substr_replace
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);

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

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

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


views 4269
  1. Add New Comment