PHP_Function (new),
Download
Report
Transcript PHP_Function (new),
String Function
Function
strcmp()
Compare two strings (case-sensitive):
strcasecmp()
Compare two strings (case-insensitive):
strlen()
returns the length of a string
substr()
function returns a part of a string
substr_count()
counts the number of times a substring
occurs in a string
strtok()
splits a string into smaller strings
(tokens).
str_split()
The str_split() function splits a string
into an array.
Function
example
strcmp()
echo strcmp("Hello world!","Hello
world!");
0
strcasecmp()
echo strcasecmp("Hello
world!","HELLO WORLD!");
0
strlen()
echo strlen("Hello");
5
substr()
echo substr("Hello world",6);
echo substr("Hello world",10)."<br>";
echo substr("Hello world",1)."<br>"
substr_count( echo substr_count("Hello world. The
)
world is nice","world");
output
World
d
ello world
2
Function
Example
Output
strtok()
$string = "Hello world.
Beautiful day today.";
$token = strtok($string, " ");
Hello
world.
Beautiful
day
today.
while ($token !== false)
{
echo "$token<br>";
$token = strtok(" ");
}
str_split()
print_r(str_split("Hello"));
Array ( [0] => H [1] => e [2] => l
[3] => l [4] => o )
Array Function
Function
sort()
sort arrays in ascending order
rsort()
- sort arrays in descending order
count()
returns the number of elements in an array.
array_key_exists()
function checks an array for a specified key, and returns
true if the key exists and false if the
key does not exist.
array_keys()
function returns an array containing the keys
Function
example
Output
sort()
$cars = array("Volvo", "BMW",
"Toyota");
sort($cars);
$clength = count($cars);
for($x = 0; $x < $clength; $x++) {
echo $cars[$x];
echo "<br>";}
BMW
Toyota
Volvo
rsort()
$cars = array("Volvo", "BMW",
"Toyota");
rsort($cars);
$clength = count($cars);
for($x = 0; $x < $clength; $x++) {
echo $cars[$x];
echo "<br>";}
Volvo
Toyota
BMW
count()
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
3
Function
example
Output
array_key_exists() $a=array("Volvo"=>"XC90","BMW"= Key exists
>"X5");
if (array_key_exists("Volvo",$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
array_keys()
$a=array("Volvo"=>"XC90","BMW"= Array ( [0] => Volvo [1]
>"X5","Toyota"=>"Highlander");
=> BMW [2] => Toyota
print_r(array_keys($a));
)