strpos
strpos
(PHP 4, PHP 5)
strpos — Find position of first occurrence of a string
Description
int strpos ( string $haystack , mixed $needle [, int $offset= 0 ] )
Returns the numeric position of the first occurrence of needle in the haystack string. Unlike the strrpos() before PHP 5, this function can take a full string as the needle parameter and the entire string will be used.
ucfirst
ucfirst :: (PHP 4, PHP 5) |
||
ucfirst — Make a string’s first character uppercase |
||
| Description | ||
string ucfirst ( string $str ) Returns a string with the first character of str capitalized, if that character is alphabetic. Note that ‘alphabetic’ is determined by the current locale. For instance, in the default “C” locale characters such as umlaut-a (ä) will not be converted. |
||
php quotes: $foo = ‘hello world!’; $foo = ucfirst($foo); // Hello world! $bar = ‘HELLO WORLD!’; :end quotes |
end
end (PHP 4, PHP 5)
end — Set the internal pointer of an array to its last element
Description
mixed end ( array &$array )
end() advances array’s internal pointer to the last element, and returns its value.
$fruits = array(’apple’, ‘banana’, ‘cranberry’);
echo end($fruits); // cranberry
empty
empty (PHP 4, PHP 5)
empty — Determine whether a variable is empty
Description
bool empty ( mixed $var )
Determine whether a variable is considered to be empty.
$var = 0;
// Evaluates to true because $var is empty
if (empty($var)) {
echo ‘$var is either 0, empty, or not set at all’;
}
// Evaluates as true because $var is set
if (isset($var)) {
echo ‘$var is set even though it is empty’;
}
count_chars
count_chars (PHP 4, PHP 5)
count_chars — Return information about characters used in a string
Description
mixed count_chars ( string $string [, int $mode] )
Counts the number of occurrences of every byte-value (0..255) in string and returns it in various ways.
$data = “Two Ts and one F.”;
foreach (count_chars($data, 1) as $i => $val) {
echo “There were $val instance(s) of “” , chr($i) , “” in the string.n”;
}
The above example will output:
There were 4 instance(s) of ” ” in the string.
There were 1 instance(s) of “.” in the string.
chop
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.
rtrim
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);
ltrim
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);
array_sum
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
array_slice
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)
chown
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.
htmlspecialchars_decode
htmlspecialchars_decode
(PHP 5 >= 5.1.0)
htmlspecialchars_decode — Convert special HTML entities back to characters
Description
string htmlspecialchars_decode ( string $string [, int $quote_style= ENT_COMPAT ] )
This function is the opposite of htmlspecialchars(). It converts special HTML entities back to characters.
The converted entities are: &, " (when ENT_NOQUOTES is not set), ' (when ENT_QUOTES is set), < and >.
strtolower
strtolower
(PHP 4, PHP 5)
strtolower — Make a string lowercase
Description
string strtolower ( string $str )
Returns string with all alphabetic characters converted to lowercase.
Note that ‘alphabetic’ is determined by the current locale. This means that in i.e. the default “C” locale, characters such as umlaut-A (Ä) will not be converted.
str_split
str_split
(PHP 5)
str_split — Convert a string to an array
Description
array str_split ( string $string [, int $split_length= 1 ] )
Converts a string to an array.
Parameters
string
The input string.
split_length
Maximum length of the chunk.
lcfirst
lcfirst
(PHP 5 >= 5.3.0)
lcfirst — Make a string’s first character lowercase
Description
string lcfirst ( string $str )
Returns a string with the first character of str , lowercased if that character is alphabetic.
Note that ‘alphabetic’ is determined by the current locale. For instance, in the default “C” locale characters such as umlaut-a (ä) will not be converted.
html_entity_decode
html_entity_decode
(PHP 4 >= 4.3.0, PHP 5)
html_entity_decode — Convert all HTML entities to their applicable characters
Description
string html_entity_decode ( string $string [, int $quote_style= ENT_COMPAT [, string $charset ]] )
html_entity_decode() is the opposite of htmlentities() in that it converts all HTML entities to their applicable characters from string .
explode
explode
(PHP 4, PHP 5)
explode — Split a string by string
Description
array explode ( string $delimiter , string $string [, int $limit ] )
Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter .
addcslashes
addcslashes
(PHP 4, PHP 5)
addcslashes — Quote string with slashes in a C style
Description
string addcslashes ( string $str , string $charlist )
Returns a string with backslashes before characters that are listed in charlist parameter.
stristr
stristr
(PHP 4, PHP 5)
stristr — Case-insensitive strstr()
Description
string stristr ( string $haystack , mixed $needle [, bool $before_needle= false ] )
Returns all of haystack from the first occurrence of needle to the end.
array_splice
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”);
array_reduce
array_reduce (PHP 4 >= 4.0.5, PHP 5)
array_reduce — Iteratively reduce the array to a single value using a callback function
Description
mixed array_reduce ( array $input, callback $function [, int $initial] )
array_reduce() applies iteratively the function function to the elements of the array input, so as to reduce the array to a single value. If the optional initial is available, it will be used at the beginning of the process, or as a final result in case the array is empty. If the array is empty and initial is not passed, array_reduce() returns NULL.
function rsum($v, $w) {
$v += $w;
return $v; }
function rmul($v, $w) {
$v *= $w;
return $v; }
$a = array(1, 2, 3, 4, 5);
$x = array();
$b = array_reduce($a, “rsum”);
$c = array_reduce($a, “rmul”, 10);
$d = array_reduce($x, “rsum”, 1);
This will result in $b containing 15, $c containing 1200 (= 10*1*2*3*4*5), and $d containing 1.
array_rand
array_rand (PHP 4, PHP 5)
array_rand — Pick one or more random entries out of an array
Description
mixed array_rand ( array $input [, int $num_req] )
array_rand() is rather useful when you want to pick one or more random entries out of an array. It takes an input array and an optional argument num_req which specifies how many entries you want to pick – if not specified, it defaults to 1.
If you are picking only one entry, array_rand() returns the key for a random entry. Otherwise, it returns an array of keys for the random entries. This is done so that you can pick random keys as well as values out of the array.
Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.
srand((float) microtime() * 10000000);
$input = array(“Neo”, “Morpheus”, “Trinity”, “Cypher”, “Tank”);
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . “n”;
echo $input[$rand_keys[1]] . “n”;
array_push
array_push (PHP 4, PHP 5)
array_push — Push one or more elements onto the end of array
Description
int array_push ( array &$array, mixed $var [, mixed $...] )
array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed. Has the same effect as:
$array[] = $var;
repeated for each var.
Returns the new number of elements in the array.
$stack = array(“orange”, “banana”);
array_push($stack, “apple”, “raspberry”);
print_r($stack);


Recent Comments