PHP Function: Built-in Functions part 12

The last of the Built-in Functions!
String Functions 
The functions I will add here only scratch the surface of PHP’s built-in string manipulation functions, but if you understand thses common functions, your programming life will be a lil easier.

addslashes() and stripslashes()
The addslashes() and stripslashes() functions are very important when inserting and retieving data from a database. Often, text inserted into a database will contain special characters such as, single quotes, double quotes, backslashes, NULL, etc. that must be escaped before being inserted. The addslashes() function does just that using this syntax:

addslashes(string);

The stripslashes() function returns a string with the slashes taken away, using this syntax:

stripslashes(string);

chop()ltrim(), and trim()
All three of these functions remove errant white space from a string. The chop() function removes white space from the end of a string, and ltrim()removes white space from the beginning of a string. The trim()function removes both leading and trailing white space from a string. Here is the syntax of these functions:

chop(string);
ltrim(string);
trim(string);

explode() and implode()
The explode() function splits a string using a given separator and returns the values in an array. The syntax of explode() is

explode(“separator”, “string”);

For example, the following code takes a string called $color_list, containing a comma-separated list of colors, and places each color into an array called $my_colors:

$color_list = “blue,black,red,green,yellow,purple”;
$my_colors = explode(“,”, $color_list);

Conversly the implode() function takes an array and makes it into a string, using a given separator. The syntax of implode() is

impload(“separator”, “string”);

For example, the following code takes an array called $color_list and then creates a string called $mycolors, containing the values of the $color_list array, separated by commas:

$mycolors = implode(“,”, $color_list);

htmlspecialchars() and htmlentities()
The htmlspecialchars() and htmlentities() functions convert special characters and HTML entities within strings into their acceptable entity representations. The htmlspecialchars() function converts only the less-than sign (< becomes “&-l-t-;”) greater than sign (> becomes “&-g-t-;”), double quotes (“” becomes “&-q-u-o-t-;”), and the ampersand (& becomes “&-a-m-p-;”).
* It does not convert the sign with the acctual “-” * 
The htmlentities() function converts the characters in the ISO-8859-1 character set to the proper HTML entity. Here is the syntax for these functions:

htmlspecialchars(string);
htmlentities(string);

nl2br()
The nl2br() function replaces all ASCII newlines with the HTML line break (<BR>).
The syntax of the nl2br() function is

nl2br(string);

sprintf()
The sprintf() function returns a string that has been formatted according to a set of directives, as listed in the table on the next page. The syntax of sprintf() is

sprintf(directives, string);

sprintf() Function Formatting Directives


Directive – Result


% – Adds a percent sign.
b – Considers the string an integer and formates it as a binary number.
c – Considers the string and integer and formats it with that ASCII value.
d – Considers the string and integer and formats it as a decimal number.
f – Considers the string a double and formats it as a floating-point number.
o – Considers the string and integer and formats it as an octal number.
s – Considers and formats the string as a string.
x – Considers the string an integer and formats it as a hexadecimal number (lowercase letters).
X – Considers the string an integer and formats it as a hexadecimal number (uppercase letters

strlen()
The strlen() function returns the length of a given string. Its syntax is:

strlen(string);

strtolower()
The strtolower() function returns a given string with all alpabetic characters in lowecase. Its syntax is

strtolower(str);

strtoupper()
The strtoupper() function returns a given string with all alphabetic characters in uppercase. Its syntax is

strtoupper(str);

substr()
The substr() function returns a portion of a string, given a starting postion and optional ultimate length. Its syntax is

substr(string, start, [length]);

If the start position is a positive number, the starting position is counted from the beginning of the string. If the start position is negative, the starting position is counted from the end of the string.

Similary, if the option length parameter is used and is a positive number, the length is counted from the beginning of the string. If the length parameter is used and is a negative number, the length is counted from the end of the string.

For example 

$new_string = substr(“PHP is great!”, 1); // returns “HP is greate!”
$new_string = substr(“PHP is great!”, 0, 7); //returns “PHP is ”
$new_string = substr(“PHP is great!”, -1); // returns “!”
$new_string = substr(“PHP is great!”, -6, 5) // returns “great”

ucfirst()
The ucfirst() function changes the first alphabetic character in a string to an uppercase character. Its syntax is

ucfirst(string);

ucwords()
The ucwords() function changes the first letter of each word in a string to uppercase. Its syntax is

ucfirst(string);

Variable Functions
The two basic vaiable functions, isset() and unset(), help you manage your variableswithin the scope of an application.

The isset() function determines whether a variable exists. The unset() function explicitly destroys the named variable. Here is the syntax of each:

isset(var);
unset(var);

The isset() function returns true if the variable exists and false if it is not.