<<

. 29
( 132 .)



>>

parts. The | character means or here; you want to match <br> or &nbsp; or any
whitespace characters. The asterisk after the closing parenthesis indicates that you
want to match any number of repeated occurrences of this pattern (for example, in
<br>&nbsp;<br>, the pattern occurs three times, and this expression would match
all of them). The final dollar sign character represents the end of the string. By
using it, you are saying that you want to match only occurrences of this pattern
that are at the string™s end, and not globally remove every whitespace character
from $body, which would likely be a bad thing.

PREG_GREP()

array preg_grep (string pattern, array input)

Given a regular expression string and an array, this function returns an array
containing only those elements of the input array that match the regular-expression
pattern.
148 Part II: Working with PHP

PREG_MATCH_ALL()

int preg_match_all (string pattern, string subject, array matches [,
int flags])

This function searches a string for matches to a regular-expression pattern. It
places the matches it finds in the matches array.

Variable functions
PHP has a number of functions that deal directly with variables and constants.
Some of these functions deal with the types of variables: These are covered in the
next section.

is_array(), is_numeric(), and is_string()
bool is_array (mixed var)

bool is_numeric (mixed var)

bool is_string (mixed var)

These three Boolean functions test whether the given variable is of a particular
type.

isset()
bool isset (mixed var [, mixed var [, ...]])

This useful function returns TRUE if every argument is an existing variable con-
taining a non-null value, and FALSE otherwise.

unset()

void unset (mixed var [, mixed var [, ...]])

This function unsets the specified variable(s).

empty()

boolean empty (mixed var)

If a variable is undefined, an empty array, or equivalent to 0 (0.00, FALSE, an
empty string, and so on); empty() returns TRUE. This code summarizes the behav-
ior of the function:
Chapter 6: PHP™s Built-in Functions 149

$a = 0 ; print empty($a) ? “TRUE” : “FALSE”; //TRUE
$b = “0” ; print empty($b) ? “TRUE” : “FALSE”; //TRUE
$c = “” ; print empty($c) ? “TRUE” : “FALSE”; //TRUE
$d = 1 ; print empty($d) ? “TRUE” : “FALSE”; //FALSE
print empty($e) ? “TRUE” : “FALSE”; //TRUE
$f= TRUE ; print empty($f) ? “TRUE” : “FALSE”; //FALSE
$g= FALSE; print empty($g) ? “TRUE” : “FALSE”; //TRUE
$h=array();print empty($h) ? “TRUE” : “FALSE”; //TRUE

floor()

float floor (float value)

Given a floating-point variable, floor() rounds down any fractional amount
and returns the highest integer value less than or equal to the value of the variable.

constant()
mixed constant (string constantname)

This function simply returns the value of a defined constant.

define()
bool define (string name, mixed value [, bool case_insensitive])

This function defines a constant with a specified name and value. If the third
argument is set to TRUE the constant will be defined as case-insensitive.

get_defined_constants()

array get_defined_constants (void)

This function returns an array containing the names and values of all the cur-
rently defined constants.

Type-conversion functions
This is a category of our own making. In the manual, these functions will fall under
other headings. However, we feel that the specialized nature of these functions
demands a unique category.
Chapter 4 discusses PHP variables in detail, including PHP™s flexible variable
typing. If you recall, if you need to evaluate a string as if it were an integer, you can
make use of the intval() function. (See Chapter 4 for similar variable-conversion
functions.)
150 Part II: Working with PHP

But at times the variable conversion will be a bit more extreme, turning strings
into arrays and arrays into strings. Why, you ask, might you want to do this?
Consider a string like the following:

24,16,9,54,21,88,17

So you have this string of integers, maybe retrieved from a text file. How would
you go about sorting it in ascending order? If you have to deal with it as a string
the code is going to get very nasty. However, if you can make use of PHP™s myriad
of array functions, life gets quite a bit easier. You can simply use the sort() func-
tion. Take a look:

$str = “24,16,9,54,21,88,17”;
//turn $str into an array
$array = explode(“,”, $str);
//sort the array in ascending order
sort($array, SORT_NUMERIC);
//turn the array back into a string and print
$new_str = implode(“,”, $array);
echo $new_str;

This will print the following:

9,16,17,21,24,54,88

More on the sort() function a bit later in the chapter.

TYPE CONVERSION FUNCTIONS USED IN THIS BOOK
The following type conversion functions are used in the examples in this book.

EXPLODE() This function transforms a string into an array.

array explode (string separator, string string [, int limit])

The first argument is the character or characters that separate the different ele-
ments. In the preceding example the string is separated by a comma. The second
argument is the string you wish to break into an array.
The third argument limits the number of elements in the resulting array. If you
use the following code

$str = “24,16,9,54,21,88,17”;
//turn $str into an array
$my_array = explode(“,”, $str, 3);
Chapter 6: PHP™s Built-in Functions 151

$my_array will have three elements: $my_array[0] => 24 $my_array[1] =>
16 $my_array[2] => 9,54,21,88,17. You can see that the last element contains
what™s left of the original string. If you want to sort only the first three elements in
a string and discard the rest you might do this:

$str = “24,16,9,54,21,88,17”;
//turn $str into an array
$array = explode(“,”, $str, 4);
unset($array[3]);
sort($array, SORT_NUMERIC);
echo implode(“,”, $array);

If the string separator does not exist, the entire string will be placed in array ele-
ment zero. If the string does not exist, an empty string will be placed in the first
element.

IMPLODE() As you might expect, implode() is the opposite of explode(): It
turns an array into a string.

string implode (string glue, array pieces)

The first argument is the string that will separate the string elements. The second
is the array to be separated.
A good example of where you might use implode() is a page that runs a SQL
delete command. Suppose you have presented a series of checkboxes to indicate
the rows you wish to delete from the database. You are probably going to want to
pass the elements you wish to delete within an array. In the script that does the
deletes, you can then run something like this:

//say $delete_items from an HTML page and
//contains (1,3,7)
if( is_array($delete_items) )
{
$str = implode(“,” , $delete_items);
$query = “delete from table where item_id in ($str)”;
mysql_query($query);
}

SPLIT() The split() function does the same thing as explode(), but it enables
you to specify a regular expression as the separation string.

array split (string pattern, string string [, int limit])
152 Part II: Working with PHP

The split() function can come into play if you want to separate a string based
on more than one element. Say you have a string you need as an array, the elements
of which can be separated by either a newline (\n) or a tab (\t). The following will
do the trick:

//note there is a tab between 524 and 879
//and a tab between 879 and 321
$items = “524 879 321
444
221”;
$array = split(“[\n\t]”, “$items”);



split() is more flexible than explode(), but it™s also slower.




PREG_SPLIT()

array preg_split (string pattern, string subject [, int limit [, int flags]])


This function works like split(), only it uses a Perl regular expression as the
pattern.
Note that if the flag is PREG_SPLIT_NO_EMPTY, empty items will not be placed in
the array.


Again, if explode() can do the same task, make sure to use it instead of
preg_split() because preg_split() is slower.




To divide a sentence into its component words (splitting by white space), you
can do the following:

$sentence = “Soup is good food.”
$words = preg_split(“\s”, $sentence);

The content of $words will be as follows:

[0] => Soup
[1] => is
Chapter 6: PHP™s Built-in Functions 153

[2] => good
[3] => food.

gettype() and settype() can be used to directly change the type of variables.

GETTYPE()

string gettype (mixed var)

This function returns the type of a variable as a string: Boolean, string, array,
integer, and so forth.

SETTYPE()

bool settype (mixed var, string type)

Given a variable as argument, this function sets the variable to the specified
type: Boolean, integer, float, string, array, object, or null.

INTVAL() AND FLOATVAL()

int intval (mixed var [, int base])

float floatval (mixed var)

These two functions take any scalar variable and return an integer or a floating-
point variable, respectively.

<<

. 29
( 132 .)



>>