"Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid." – Albert Einstein

PHP Functions 4



31. array_slice (PHP 4, PHP 5)
array_slice — Extract a slice of the array
Description
array array_slice ( array $array, int $offset [, int $length [, bool $preserve_keys]] )
array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters.
If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array.

If length is given and is positive, then the sequence will have that many elements in it. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array.
Note that array_slice() will reorder and reset the array indices by default. Since PHP 5.0.2, you can change this behaviour by setting preserve_keys to TRUE.

$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2); // returns "c", "d", and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"
print_r(array_slice($input, 2, -1)); // note the differences in the array keys
print_r(array_slice($input, 2, -1, true));

The above example will output:
Array([0] => c[1] => d)
Array([2] => c[3] => d)


32. array_splice (PHP 4, PHP 5)
array_splice — Remove a portion of the array and replace it with something else
Description
array array_splice ( array &$input, int $offset [, int $length [, array $replacement]] )

array_splice() removes the elements designated by offset and length from the input array, and replaces them with the elements of the replacement array, if supplied. It returns an array containing the extracted elements. Note that numeric keys in input are not preserved.
If offset is positive then the start of removed portion is at that offset from the beginning of the input array. If offset is negative then it starts that far from the end of the input array.

If length is omitted, removes everything from offset to the end of the array. If length is specified and is positive, then that many elements will be removed. If length is specified and is negative then the end of the removed portion will be that many elements from the end of the array. Tip: to remove everything from offset to the end of the array when replacement is also specified, use count($input) for length.

If replacement array is specified, then the removed elements are replaced with elements from this array. If offset and length are such that nothing is removed, then the elements from the replacement array are inserted in the place specified by the offset. Note that keys in replacement array are not preserved. If replacement is just one element it is not necessary to put array() around it, unless the element is an array itself.
The following statements change the values of $input the same way:
Table 21. array_splice() equivalents
array_push($input, $x, $y) array_splice($input, count($input), 0, array($x, $y))
array_pop($input) array_splice($input, -1)
array_shift($input) array_splice($input, 0, 1)
array_unshift($input, $x, $y) array_splice($input, 0, 0, array($x, $y))
$input[$x] = $y // for arrays where key equals offset array_splice($input, $x, 1, $y)

Returns the array consisting of removed elements.
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("red", "green")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input is now array("red", "yellow")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
// $input is now array("red", "orange")

$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
// $input is now array("red", "green",
// "blue", "black", "maroon")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
// $input is now array("red", "green",
// "blue", "purple", "yellow");



33. array_sum (PHP 4 >= 4.0.4, PHP 5)
array_sum — Calculate the sum of values in an array
Description
number array_sum ( array $array )
array_sum() returns the sum of values in an array as an integer or float.
$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum($a) . "n";

$b = array("a" => 1.2, "b" => 2.3, "c" => 3.4);
echo "sum(b) = " . array_sum($b) . "n";

The above example will output:
sum(a) = 20
sum(b) = 6.9


34. array_unshift (PHP 4, PHP 5)
array_unshift — Prepend one or more elements to the beginning of an array
Description
int array_unshift ( array &$array, mixed $var [, mixed $…] )
array_unshift() prepends passed elements to the front of the array. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order. All numerical array keys will be modified to start counting from zero while literal keys won’t be touched.
Returns the new number of elements in the array.
$queue = array("orange", "banana");
array_unshift($queue, "apple", "raspberry");
print_r($queue);
The above example will output:
Array([0] => apple[1] => raspberry[2] => orange[3] => banana)


35. trim (PHP 4, PHP 5)
trim — Strip whitespace (or other characters) from the beginning and end of a string
Description
string trim ( string $str [, string $charlist] )
This function returns a string with whitespace stripped from the beginning and end of str. Without the second parameter, trim() will strip these characters:
• " " (ASCII 32 (0x20)), an ordinary space.
• "t" (ASCII 9 (0x09)), a tab.
• "n" (ASCII 10 (0x0A)), a new line (line feed).
• "r" (ASCII 13 (0x0D)), a carriage return.
• "�" (ASCII 0 (0x00)), the NUL-byte.
• "x0B" (ASCII 11 (0x0B)), a vertical tab.
$text = "ttThese are a few words 🙂 … ";
$binary = "x09Example stringx0A";
$hello = "Hello World";
var_dump($text, $binary, $hello);
print "n";
$trimmed = trim($text);
var_dump($trimmed);
$trimmed = trim($text, " t.");
var_dump($trimmed);
$trimmed = trim($hello, "Hdle");
var_dump($trimmed);
// trim the ASCII control characters at the beginning and end of $binary
// (from 0 to 31 inclusive)
$clean = trim($binary, "x00..x1F");
var_dump($clean);

The above example will output:

string(32) " These are a few words 🙂 … "
string(16) " Example string
"
string(11) "Hello World"

string(28) "These are a few words 🙂 …"
string(24) "These are a few words :)"
string(5) "o Wor"
string(14) "Example string"


36. ltrim (PHP 4, PHP 5)
ltrim — Strip whitespace (or other characters) from the beginning of a string
Description
string ltrim ( string $str [, string $charlist] )
Strip whitespace (or other characters) from the beginning of a string.
$text = "ttThese are a few words 🙂 … ";
$binary = "x09Example stringx0A";
$hello = "Hello World";
var_dump($text, $binary, $hello);

print "n";

$trimmed = ltrim($text);
var_dump($trimmed);

$trimmed = ltrim($text, " t.");
var_dump($trimmed);

$trimmed = ltrim($hello, "Hdle");
var_dump($trimmed);

// trim the ASCII control characters at the beginning of $binary
// (from 0 to 31 inclusive)
$clean = ltrim($binary, "x00..x1F");
var_dump($clean);

The above example will output:
string(32) " These are a few words 🙂 … "
string(16) " Example string
"
string(11) "Hello World"

string(30) "These are a few words 🙂 … "
string(30) "These are a few words 🙂 … "
string(7) "o World"
string(15) "Example string
"


37. rtrim (PHP 4, PHP 5)
rtrim — Strip whitespace (or other characters) from the end of a string
Description
string rtrim ( string $str [, string $charlist] )
This function returns a string with whitespace stripped from the end of str.
$text = "ttThese are a few words 🙂 … ";
$binary = "x09Example stringx0A";
$hello = "Hello World";
var_dump($text, $binary, $hello);

print "n";

$trimmed = rtrim($text);
var_dump($trimmed);
$trimmed = rtrim($text, " t.");
var_dump($trimmed);
$trimmed = rtrim($hello, "Hdle");
var_dump($trimmed);
// trim the ASCII control characters at the end of $binary
// (from 0 to 31 inclusive)
$clean = rtrim($binary, "x00..x1F");
var_dump($clean);

The above example will output:

string(32) " These are a few words 🙂 … "
string(16) " Example string
"
string(11) "Hello World"

string(30) " These are a few words 🙂 …"
string(26) " These are a few words :)"
string(9) "Hello Wor"
string(15) " Example string"



38. chop (PHP 4, PHP 5)
chop — Alias of rtrim()
Description
This function is an alias of: rtrim().
Notes
Note: chop() is different than the Perl chop() function, which removes the last character in the string.

39. chown (PHP 4, PHP 5)
chown — Changes file owner
Description
bool chown ( string $filename, mixed $user )
Attempts to change the owner of the file filename to user user. Only the superuser may change the owner of a file.
Notes
Note: This function will not work on remote files as the file to be examined must be accessible via the servers filesystem.
Note: When safe mode is enabled, PHP checks whether the files or directories you are about to operate on have the same UID (owner) as the script that is being executed.


40. chr (PHP 4, PHP 5)
chr — Return a specific character
Description
string chr ( int $ascii )
Returns a one-character string containing the character specified by ascii.
This function complements ord().
$str = "The string ends in escape: ";
$str .= chr(27); /* add an escape character at the end of $str */
/* Often this is more useful */
$str = sprintf("The string ends in escape: %c", 27);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s