1. substr() definition: The substr()
function returns a part of a string.
Usage: If you know where the information that you want lies in a larger string, you can extract it out with the substr()
function.substr function signature substr(string, start, length)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to return a part of |
start | Mandatory. Specifies where to start in the string
|
length | Not Mandatory. Specifies the length of the returned string. Default is to the end of the string.
|
Code Example
1
2
3
4
5
6
7
8
|
<?php
$sentence = "The Apple iPhone 6 Plus is cool, just don't bend it";
$one = substr($sentence, 9, 10);
$two = substr($sentence, 24, 27);
echo $one.' - ';
echo $two;
?>
|
iPhone 6 – is cool, just don’t bend it
2. strlen() definition: The strlen()
function returns to us the number of characters in the string it’s given.
Usage: There are many times when you will need to know the length of a string. The strlen()
function makes this super easy for us.strlen function signature strlen(string)
Argument | Argument Meaning |
---|---|
string | Mandatory. This is the string that we will check the length of. |
Code Example
1
2
3
4
5
6
7
|
<?php
$length = strlen("What is the length of Ariana Grande, Kate Upton, Nicki Minaj, and Selena Gomez?");
echo $length;
?>
|
79
3. sprintf() definition: The sprintf()
function returns a string created by filling format with the given arguments.
Usage: The sprintf()
function can be used to assign a formatted string to a variable. The arg1, arg2, ++ parameters are inserted at the percent (%) signs in the main string. sprintf()
works in a step by step fashion meaning at the first % sign, arg1 is inserted, at the second % sign, arg2 is inserted, and so on.sprintf function signature sprintf(format, arg1, arg2, arg++)
Argument | Argument Meaning |
---|---|
format | Mandatory. Specifies the string and how to format the variables in it.
Possible format values:
|
arg1 | Mandatory. The argument to be inserted at the first %-sign in the format string |
arg2 | Not Mandatory. The argument to be inserted at the second %-sign in the format string |
arg++ | Not Mandatory. The argument to be inserted at the third, fourth, etc. %-sign in the format string |
Code Example
1
2
3
4
5
6
7
8
|
<?php
$first_name = 'President';
$last_name = 'Business';
$hello = sprintf("Hello there, %s %s", $first_name, $last_name);
echo $hello;
?>
|
Hello there, President Business
4. echo() definition: The echo()
function outputs strings and information to the browser.
Usage: You’ll make use of echo()
constantly. Technically, echo()
is not actually a function but a language construct. As such, in almost all use cases you will omit the parenthesis entirely.echo function signature echo(strings)
Argument | Argument Meaning |
---|---|
strings | Mandatory. One or more strings to be sent to the output |
Code Example
1
2
3
4
5
6
7
8
9
|
<?php
$verb = 'am chilling';
$state = 'cool';
$person = 'Mr. Brickowski';
echo "I $verb with a Snapple Iced Tea.<br>";
echo "Isn't that pretty $state?<br>";
echo "<span class='label label-info'>Why yes, Yes it is $person.</span></div>";
?>
|
I am chilling with a Snapple Iced Tea.
Isn’t that pretty cool?
Why yes, Yes it is Mr. Brickowski.
This small snippet shows how we can echo out strings to the screen.
We used double quotes to make use of interpolation for these examples.Isn’t that pretty cool?
Why yes, Yes it is Mr. Brickowski.
5. strpos() definition: The strpos()
function returns the position of the first occurrence of find in string. If specified, the function begins its search at position start. Returns false if find is not found.
Usage: You can use the strpos()
function to find the first occurrence of a small string in a larger string and if the small string isn’t found, strpos()
returns false.strpos function signature strpos(string, find, start)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to search |
find | Mandatory. Specifies the string to find |
start | Not Mandatory. Specifies where to begin the search |
Code Example
1
2
3
4
5
6
7
8
9
10
|
<?php
$string = '0123456789';
$find = '7';
$start = 0;
$result = strpos($string, $find, $start);
echo $result;
?>
|
7
The code above uses a string of characters that represent numbers as a
string. These are not real numbers, they are of type string not int or
float! We simply use this to illustrate that when using this function
the very first character in a string starts with zero. For every single
character including any whitespace, the number increments by one. We
start at the first position of 0 in the example, but had we started at 8
for example, then the function would have returned false since it was
looking for 7, but started looking at the eighth position so it already
missed it.
6. implode() definition: The implode()
function does the exact opposite of explode()
– it creates a large string from an array of smaller strings.
Usage: Returns a string created by joining every element in the array with separator.implode function signature implode(separator, array)
Argument | Argument Meaning |
---|---|
separator | Not Mandatory. Specifies what to put between the array elements. Default is “” (an empty string) |
array | Required. The array to join to a string |
Code Example
1
2
3
4
5
6
7
8
|
<?php
$fields = array('iPhone', 'Macbook', 'Lenovo Ultrabook', 'Vacation at Cape Cod');
$string = implode(' @@ ', $fields);
echo $string;
?>
|
iPhone @@ Macbook @@ Lenovo Ultrabook @@ Vacation at Cape Cod
7. str_replace() definition: The str_replace()
function replaces some characters with some other characters in a string.
Usage: Searches for all occurrences of find in string and replaces them with replace.
If all three parameters are strings, a string is returned. If string is
an array, the replacement is performed for every element in the array
and an array of results is returned. If find and replace are both arrays, elements in find are replaced with the elements in replace with the same numeric indices. Finally, if find is an array and replace is a string, any occurrence of any element in find is changed to replace. If supplied, count is filled with the number of instances replaced.str_replace function signature str_replace(find, replace, string, count)
Argument | Argument Meaning |
---|---|
find | Mandatory. Specifies the value to find |
replace | Mandatory. Specifies the value to replace the value in find |
string | Mandatory. Specifies the string to be searched |
count | Not Mandatory. A variable that counts the number of replacements |
Code Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?php
$strings = array (
'You like to have a great experience',
'You are a really nice individual',
'Do you like to drink Dunkin Donuts?'
);
$search = array (
'great',
'experience',
'individual',
'Dunkin Donuts'
);
$replace = array (
'fantastic',
'time',
'person',
'Starbucks'
);
$replaced = str_replace ( $search, $replace, $strings );
print_r ( $replaced );
?>
|
Array
(
[0] => You like to have a fantastic time
[1] => You are a really nice person
[2] => Do you like to drink Starbucks?
)
(
[0] => You like to have a fantastic time
[1] => You are a really nice person
[2] => Do you like to drink Starbucks?
)
8. strtolower() definition: The strtolower()
function turns a mixed case string into all lowercase characters.
Usage: Returns string with all alphabetic characters converted to lowercase. The table used for converting characters is locale-specific.strtolower function signature strtolower(string)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string that we will convert to all lowercase. |
Code Example
1
2
3
4
5
6
7
8
9
10
|
<?php
function chill($string) {
return strtolower ( $string );
}
$person = 'BUDDY CAN YOU TURN OFF THE CAPS LOCK KEY?!';
echo chill ( $person );
?>
|
buddy can you turn off the caps lock key?!
9. explode() definition: The explode()
function breaks apart a string into an array.
Usage: If you know where the information that you want lies in a larger string, you can extract it out with the explode()
function.explode function signature explode(separator, string, limit)
Argument | Argument Meaning |
---|---|
separator | Mandatory. Specifies where to break the string |
string | Mandatory. The string to split |
limit | Not Mandatory. Specifies the number of array elements to return.
Possible values:
|
Code Example
1
2
3
4
|
<?php
$string = "Your mind is so prodigiously empty that there is simply nothing left to empty out.";
print_r (explode(' ',$string));
?>
|
Array
(
[0] => Your
[1] => mind
[2] => is
[3] => so
[4] => prodigiously
[5] => empty
[6] => that
[7] => there
[8] => is
[9] => simply
[10] => nothing
[11] => left
[12] => to
[13] => empty
[14] => out.
)
(
[0] => Your
[1] => mind
[2] => is
[3] => so
[4] => prodigiously
[5] => empty
[6] => that
[7] => there
[8] => is
[9] => simply
[10] => nothing
[11] => left
[12] => to
[13] => empty
[14] => out.
)
10. chr() definition: The chr()
function returns a character when you pass it an ASCII value.
Usage: Returns a string consisting of the single ASCII character ascii.chr function signature chr(ascii)
Argument | Argument Meaning |
---|---|
ascii | Mandatory. An ASCII value |
Code Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php
// Decimal value
echo chr(33);
echo chr(34);
echo chr(35);
// Octal values
echo chr(110);
echo chr(111);
echo chr(112);
// Hex values
echo chr(0x52)
?>
|
!”#nopR
11. trim() definition: The trim()
function cleans a string of all whitespace or other specified characters.
Usage: Returns string with every whitespace character in charlist stripped from the beginning and end of the string. You can specify a range of characters to strip using ..
within the string. For example, a..z
would strip each lowercase alphabetical character. If charlist is not supplied, n, r, t, x0B, , and spaces are stripped.trim function signature trim(string, charlist)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to check |
charlist | Not Mandatory. Specifies which characters to remove from the string. If left out, all of the following characters are removed:
|
Code Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php
$text = ('asdf This is a
cool string with some nonsense characters
at the start and end,
not to mention some really strange whitespace characters
asdf');
$slim_n_trim = trim($text, 'asdf');
echo $slim_n_trim;
?>
|
This is a cool string with some
nonsense characters at the start and end, not to mention some really
strange whitespace characters
12. str_repeat() definition: The str_repeat()
function repeats a string a specified number of times..
Usage: Returns a string consisting of repeat copies of string appended to each other. If repeat is not greater than 0, an empty string is returned.str_repeat function signature substr(string, repeat)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to repeat |
repeat | Mandatory. Specifies the number of times the string will be repeated. Must be greater or equal to 0 |
Code Example
1
2
3
|
<?php
echo str_repeat('Everything is Awesome! ', 3);
?>
|
Everything is Awesome! Everything is Awesome! Everything is Awesome!
13. strtoupper() definition: The strtoupper()
function returns a string with all of it’s characters converted to uppercase.
Usage: If you know where the information that you want lies in a larger string, you can extract it out with the strtoupper()
function.strtoupper function signature strtoupper(string)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to be converted to uppercase. |
Code Example
1
2
3
4
5
6
|
<?php
$tired = "hey buddy, sorry I didn't get my caffeine yet";
$starbucks = ", but hold up - i'll drink some now.... ... ten minutes later...";
$awesome = strtoupper(" wow I feel awesome!!!");
echo $tired.$starbucks.$awesome;
?>
|
hey buddy, sorry I didn’t get my caffeine yet, but hold up – i’ll drink some now…. … ten minutes later… WOW I FEEL AWESOME!!!
14. str_pad() definition: The str_pad()
function pads a string to a new length.
Usage: Pads string using pad_string until it is at least length characters and returns the resulting string. By specifying pad_type, you can control where the padding occurs..str_pad function signature str_pad(string, length, pad_string, pad_type)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to pad |
length | Mandatory. Specifies the new string length. If this value is less than the original length of the string, nothing will be done |
pad_string | Not Mandatory. Specifies the string to use for padding. Default is whitespace |
pad_type | Not Mandatory. Specifies what side to pad the string.
Possible values:
|
Code Example
1
2
3
|
<?php
echo str_pad('Microsoft owns Minecraft ', 53, 'wow!');
?>
|
Microsoft owns Minecraft wow!wow!wow!wow!wow!wow!wow!
15. ord() definition: The ord()
function returns the ASCII value of the first character of a string.
Usage: Returns the ASCII value of the first character in string.ord function signature ord(string)
Argument | Argument Meaning |
---|---|
string | Mandatory. The string to get an ASCII value from |
Code Example
1
2
3
4
5
6
7
|
<?php
$one = ord('The best way to get an iPhone is to visit your friendly Apple Store');
$two = ord('t');
$three = ord('Hello there good buddy');
echo "$one | $two | $three";
?>
|
84 | 116 | 72
16. strtok() definition: The strtok()
function splits a string into smaller strings called tokens.
Usage: Breaks string into tokens separated by any of the characters in split and returns the nexttoken found. The first time you call
strtok()
on a string,
use the first function prototype; afterward, use the second, providing
only the tokens. The function contains an internal pointer for each
string it is called with.strtok function signature strtok(string, split) and strtok(split)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to split |
split | Mandatory. Specifies one or more split characters |
Code Example
1
2
3
4
5
6
7
8
9
10
|
<?php
$string = "Please make sure to follow the instructions (or you'll be put to sleep).";
$token = strtok($string, " ");
while ($token !== false)
{
echo "$token<br>";
$token = strtok(" ");
}
?>
|
Please
make
sure
to
follow
the
instructions
(or
you’ll
be
put
to
sleep).
make
sure
to
follow
the
instructions
(or
you’ll
be
put
to
sleep).
17. strtr() definition: The strtr()
function translates certain characters in a string.
Usage: When given three arguments, returns a string created by translating in string every occurrence of a character in from to the character in to with the same position. When given two arguments, returns a string created by translating occurrences of the keys in array in string with the corresponding values in array.strtr function signature strtr(string, from, to) and strtr(string, array)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to translate |
from | Required (unless array is used). Specifies what characters to change |
to | Required (unless array is used). Specifies what characters to change into |
array | Required (unless to and from is used). An array containing what to change from as key, and what to change to as value |
Code Example
1
2
3
4
|
<?php
$array = array("tech" => "help", "support" => "right now");
echo strtr("Do you need some tech support?",$array);
?>
|
Do you need some help right now?
18. rtrim() definition: The rtrim()
function removes whitespace or other predefined characters from the right side of a string.
Usage: Returns string with all characters in charlist stripped from the end. If charlist is not specified, the characters stripped are n, r, t, v, , and spaces..rtrim function signature rtrim(string, charlist)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to check |
charlist | Not Mandatory. Specifies which characters to remove from the string. If left out, all of the following characters are removed:
|
Code Example
1
2
3
4
|
<?php
$right_trimmed = rtrim('The prophecy states you are the most interesting person in the world. cANyOUbELIEVEtHAT?', 'cANyOUbELIEVEtHAT?');
echo $right_trimmed;
?>
|
The prophecy states you are the most interesting person in the world.
19. md5() definition: The md5()
function calculates the MD5 hash of a string.
Usage: Calculates the MD5 encryption hash of string and returns it. If the raw option is true
then the MD5 hash returned is in raw binary format (length of 16), binary defaults to false
, thus making MD5 return a full 32-character hex string.md5 function signature md5(string, raw)
Argument | Argument Meaning |
---|---|
string | Mandatory. The string to be calculated |
raw | Not Mandatory. Specifies hex or binary output format:
|
Code Example
1
2
3
4
|
<?php
$str = "Soon, I will be a collection of characters you could never possibly understand.";
echo md5($str);
?>
|
78e382eb9b9b8c265bb10d3bce8e1608
20. strrpos() definition: The strrpos()
function finds the position of the last occurrence of a string inside another string.
Usage: Returns the position of the last occurrence of find in string, or false if find is not found.If specified and positive, the search begins start characters from the start of string. If specified and negative, the search begins start characters from the end of string.
strrpos function signature strrpos(string, find, start)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to search |
find | Mandatory. Specifies the string to find |
start | Not Mandatory. Specifies where to begin the search |
Code Example
1
2
3
4
|
<?php
$position = strrpos("Everything is awesome, everything is cool when your part of a team, everything is awesome, when you're living the dream!", "awesome");
echo "The last position of 'awesome' is at position $position";
?>
|
The last position of ‘awesome’ is at position 82
21. join() definition: The join()
function returns a string from the elements of an array.
Usage: The join()
function is simply an alias for implode()
.join function signature join(separator, array)
Argument | Argument Meaning |
---|---|
separator | Not Mandatory. Specifies what to put between the array elements. Default is “” (an empty string) |
array | Mandatory. The array to join to a string |
Code Example
1
2
3
4
5
6
|
<?php
$fields = array('This', 'does', 'the same', 'thing as implode()');
$string = join(' @@ ', $fields);
echo $string;
?>
|
This @@ does @@ the same @@ thing as implode()
22. bin2hex() definition: The bin2hex()
function returns a part of a string.
Usage: Converts string
to a hexadecimal (base-16) value. Up to a 32-bit number, or 2,147,483,647 decimal, can be converted.bin2hex function signature bin2hex(string)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to be converted to hex |
Code Example
1
2
3
4
|
<?php
$hex = bin2hex('I am now nothing more than hex');
echo $hex;
?>
|
4920616d206e6f77206e6f7468696e67206d6f7265207468616e20686578
23. ltrim() definition: The ltrim()
function removes whitespace or other predefined characters from the left side of a string.
Usage: Returns string with all characters in charlist stripped from the beginning. If charlist is not specified, the characters stripped are n, r, t, v, , and spaces..ltrim function signature ltrim(string, charlist)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to check |
charlist | Not Mandatory. Specifies which characters to remove from the string. If left out, all of the following characters are removed:
|
Code Example
1
2
3
4
|
<?php
$left_trimmed = ltrim('cANyOUbELIEVEtHAT? The prophecy states you are the most interesting person in the world. ', 'cANyOUbELIEVEtHAT?');
echo $left_trimmed;
?>
|
The prophecy states you are the most interesting person in the world.
24. substr_replace() definition: The substr_replace()
function replaces a part of a string with another string.
Usage: Replaces a substring in string with replacement. The substring replaced is selected using the same rules as those of substr()
.
If string is an array, replacements take place on each string within
the array. In this case, replacement, start, and length can either be
scalar values, which are used for all strings in string, or arrays of values to be used for each corresponding value in string.substr_replace function signature substr_replace(string, replacement, start, length)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to check |
replacement | Mandatory. Specifies the string to insert |
start | Mandatory. Specifies where to start replacing in the string
|
length | Not Mandatory. Specifies how many characters should be replaced. Default is the same length as the string.
|
Code Example
1
2
3
4
5
6
7
8
9
10
|
<?php
$greet = "Goooooood Morning Vietnam!";
$nyc = substr_replace($greet, "New York!", 18, 7);
echo $nyc.'<br>';
$boston = substr_replace($nyc, "Boston", 18, 8);
echo $boston;
?>
|
Goooooood Morning New York!!
Goooooood Morning Boston!!
Goooooood Morning Boston!!
25. str_split() definition: The str_split()
function splits a string into an array.
Usage: Splits string into an array of characters, each containing length characters; if length is not specified, it defaults to 1.str_split function signature str_split(string, start, length)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to split |
length | Not Mandatory. Specifies the length of each array element. Default is 1 |
Code Example
1
2
3
|
<?php
print_r(str_split('SPACESHIP!'));
?>
|
Array
(
[0] => S
[1] => P
[2] => A
[3] => C
[4] => E
[5] => S
[6] => H
[7] => I
[8] => P
[9] => !
)
(
[0] => S
[1] => P
[2] => A
[3] => C
[4] => E
[5] => S
[6] => H
[7] => I
[8] => P
[9] => !
)
26. sha1() definition: The sha1()
function returns a part of a string.
Usage: Calculates the sha1 encryption hash of string and returns it. If raw is set and is true, theraw binary is returned instead of a hex string.
sha1 function signature sha1(string, raw)
Argument | Argument Meaning |
---|---|
string | Mandatory. The string to be calculated |
raw | Not Mandatory. Specify hex or binary output format:
|
Code Example
1
2
3
4
5
6
|
<?php
$password = sha1('password');
echo "Hey bud, what's your password? You: Oh it's $password. - Let me know if you have any trouble logging in.";
?>
|
Hey bud, what’s your password?
You: Oh it’s 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8. – Let me know
if you have any trouble logging in.
27. ucfirst() definition: The ucfirst()
function returns a part of a string.
Usage: Returns string with the first character, if alphabetic, converted to uppercase. The table used for converting characters is locale-specific.ucfirst function signature ucfirst(string)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to apply an uppercase first letter to. |
Code Example
1
2
3
4
5
6
7
|
<?php
$lcfirst = "super cool";
$ucfirst = ucfirst($lcfirst);
echo $ucfirst;
?>
|
Super cool
28. chunk_split() definition: The chunk_split()
function splits a string into a series of smaller parts.
Usage: If you know where the information that you want lies in a larger string, you can extract it out with the chunk_split()
function.chunk_split function signature chunk_split(string, length, end)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to split |
length | Not Mandatory. A number that defines the length of the chunks. Default is 76 |
end | Not Mandatory. A string that defines what to place at the end of each chunk. Default is rn |
Code Example
1
2
3
4
5
6
7
|
<?php
$sentence = "Eat the Leafy Greens for good health.";
$chunked = chunk_split($sentence, 3, ':-)');
echo $chunked;
?>
|
Eat:-) th:-)e L:-)eaf:-)y G:-)ree:-)ns :-)for:-) go:-)od :-)hea:-)lth:-).:-)
29. ucwords() definition: The ucwords()
function converts the first character of each word in a string to uppercase.
Usage: If you’d like to turn the first character of every word into uppercase, this function will do that for you.ucwords function signature ucwords(string)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to convert to uppercase for the first character of each word. |
Code Example
1
2
3
4
5
6
7
|
<?php
$title = "ten reasons to check out the latest article on something!";
$titlized = ucwords($title);
echo $titlized;
?>
|
Ten Reasons To Check Out The Latest Article On Something!
30. htmlspecialchars() definition: The htmlspecialchars()
function converts some predefined characters to HTML entities.
Usage: When you need to convert actual HTML into the entities that make up that HTML, this function can do that for you.htmlspecialchars function signature htmlspecialchars(string, flags, character-set, double_encode)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to convert |
flags | Not Mandatory. Specifies how to handle quotes, invalid encoding and the used document type.
The available quote styles are:
|
character-set | Not Mandatory. A string that specifies which character-set to use.
Allowed values are:
|
double_encode | Not Mandatory. A boolean value that specifies whether to encode existing html entities or not.
|
Code Example
1
2
3
4
5
|
<?php
$entity = htmlspecialchars('<b>Look at me, I am bold</b>. No, no actually you are not, you have been special chared.');
echo $entity;
?>
|
<b>Look at me, I am bold</b>. No, no actually you are not, you have been special chared.
31. stripos() definition: The stripos()
function finds the position of the first occurrence of a string inside another string.
Usage: Returns the position of the first occurrence of find in string using case-insensitive comparison. If specified, the function begins its search at position start. Returns false if find is not found.stripos function signature stripos(string, find, start)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to search |
find | Mandatory. Specifies the string to find |
start | Not Mandatory. Specifies where to begin the search |
Code Example
1
2
3
4
5
6
|
<?php
$string = 'Just where does that color appear in the string?';
$occurs = stripos($string,"COLOR");
echo "You're in luck because with stripos() I can tell you it is at $occurs";
?>
|
You’re in luck because with stripos() I can tell you it is at 21
32. substr_count() definition: The substr_count()
function counts the number of times a substring occurs in a string.
Usage: Returns the number of times substring appears in string. If start is provided, the search begins at that character offset for at most length characters, or until the end of the string if length is not provided.substr_count function signature substr_count(string, substring, start, length)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to check |
substring | Mandatory. Specifies the string to search for |
start | Not Mandatory. Specifies where in string to start searching |
length | Not Mandatory. Specifies the length of the search |
Code Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?php
$string = "Have you heard the news? Everyone’s talkin'
Life is good ‘cause everything awesome
Lost my job, there’s a new opportunity
More free time for my awesome community
I feel more awesome than an awesome possum
Dip my body in chocolate frostin'
Three years later wash off the frostin'
Smellin’ like a blossom, everything is awesome
Stepped in mud, got some new brown shoes
It’s awesome to win and it’s awesome to lose";
$awesome = substr_count($string, 'awesome');
echo "How about that, you said awesome $awesome times!";
?>
|
How about that, you said awesome 7 times!
33. number_format() definition: The number_format()
function formats a number with grouped thousands.
Usage: Creates a string representation of number. If decimals is given, the number is rounded to that many decimal places; the default is no decimal places, creating an integer. If decimalpoint and separator are provided, they are used as the decimal-place character and thousands separator, respectively.number_format function signature number_format(number, decimals, decimalpoint, separator)
Argument | Argument Meaning |
---|---|
number | Mandatory. The number to be formatted. If no other parameters are set, the number will be formatted without decimals and with comma (,) as the thousands separator. |
decimals | Not Mandatory. Specifies how many decimals. If this parameter is set, the number will be formatted with a dot (.) as decimal point |
decimalpoint | Not Mandatory. Specifies what string to use for decimal point |
separator | Not Mandatory. Specifies what string to use for thousands separator. Only the first character of separator is used. For example, “xxx” will give the same output as “x” Note: If this parameter is given, all other parameters are required as well |
Code Example
1
2
3
4
5
6
7
8
|
<?php
$cash = 5500.257612345;
$savings = number_format($cash, 2);
echo "The company has $savings in reserve.";
?>
|
The company has 5,500.26 in reserve.
34. stripcslashes() definition: The stripcslashes()
function removes backslashes added by the addcslashes() function.
Usage: When you need to clean up data retrieved from a database or from an HTML form, this function works great.stripcslashes function signature stripcslashes(string)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to clean. |
Code Example
1
2
3
4
5
6
7
8
|
<?php
$dirty = "Friend what you have is one dirty string";
$clean = stripcslashes($dirty);
echo $clean;
?>
|
Friend what you have is one dirty string
35. strstr() definition: The strstr()
function searches for the first occurrence of a string inside another string.
Usage: Returns the portion of string from the first occurrence of search until the end of string, or from the first occurrence of search until the beginning of string if before_search is specifiedand true. If search is not found, the function returns false. If search contains more than one character, only the first is used.
strstr function signature strstr(string, search, before_search)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to search |
search | Mandatory. Specifies the string to search for. If this parameter is a number, it will search for the character matching the ASCII value of the number |
before_search | Not Mandatory. A boolean value whose default is “false”. If set to “true”, it returns the part of the string before the first occurrence of the search parameter. |
Code Example
1
2
3
4
5
|
<?php
$sentence = "PHP is a great open source software community.";
echo strstr($sentence, 'great');
?>
|
great open source software community.
36. wordwrap() definition: The wordwrap()
function wraps a string into new lines when it reaches a specific length.
Usage: Inserts break into string every width
characters and at the end of the string and returns the resulting
string. While inserting breaks, the function attempts to not break in
the middle of a word. If not specified, break defaults to n and size defaults to 75. If cut is given and is true, the string is always wrapped to the given widthwordwrap function signature wordwrap(string, width, break, cut)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to break up into lines |
width | Not Mandatory. Specifies the maximum line width. Default is 75 |
break | Not Mandatory. Specifies the characters to use as break. Default is “n” |
cut | Not Mandatory. Specifies whether words longer than the specified width should be wrapped:
|
Code Example
1
2
3
4
|
<?php
$str = "Sometimes you will have a string that is quite long and in cases like that you can use the wordwrap function to make it easier to work with.";
echo wordwrap($str,30,"<br>n");
?>
|
Sometimes you will have a
string that is quite long and
in cases like that you can use
the wordwrap function to make
it easier to work with.
string that is quite long and
in cases like that you can use
the wordwrap function to make
it easier to work with.
37. addcslashes() definition: The addcslashes()
function returns a string with backslashes in front of the specified characters.
Usage: Returns escaped instances of characters in string by adding a backslash before them. Youcan specify ranges of characters by separating them with two periods; for example, to escape characters between a and q, use "a..q". Multiple characters and ranges can be specified in characters.
addcslashes function signature addcslashes(string, characters)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to be escaped |
characters | Mandatory. Specifies the characters or range of characters to be escaped |
Code Example
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php
$str = "Hi Traveler, thanks for stopping by!";
echo $str."<br>";
echo addcslashes($str,'t')."<br>";
echo addcslashes($str,'e')."<br>";
$str = "Green energy is getting more important everyday!";
echo $str."<br>";
echo addcslashes($str,'A..Z')."<br>";
echo addcslashes($str,'a..z')."<br>";
echo addcslashes($str,'a..g');
?>
|
Hi Traveler, thanks for stopping by!
Hi Traveler, thanks for stopping by!
Hi Traveler, thanks for stopping by!
Green energy is getting more important everyday!
Green energy is getting more important everyday!
Green energy is getting more important everyday!
Green energy is getting more important everyday!
Hi Traveler, thanks for stopping by!
Hi Traveler, thanks for stopping by!
Green energy is getting more important everyday!
Green energy is getting more important everyday!
Green energy is getting more important everyday!
Green energy is getting more important everyday!
38. parse_str() definition: The parse_str()
function parses a query string into variables.
Usage: Parses string as if coming from an HTTP POST request, setting variables in the local scopeto the values found in the string. If array is given, the array is set with keys and values from the string.
parse_str function signature parse_str(string, array)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to parse |
array | Not Mandatory. Specifies the name of an array to store the variables. This parameter indicates that the variables will be stored in an array. |
Code Example
1
2
3
4
5
6
|
<?php
parse_str("color=Blue&make=Subaru&model=wrx");
echo $color."<br>";
echo $make."<br>";
echo $model;
?>
|
Blue
Subaru
wrx
Subaru
wrx
39. strcmp() definition: The strcmp()
function compares two strings.
Usage:
Compares two strings; returns a number less than 0 if stringone is less
than stringtwo, 0 if the two strings are equal, and a number greater
than 0 if stringone is greater than stringtwo. The comparison is case
sensitive meaning, “Alphabet” and “alphabet” are not considered equal.strcmp function signature strcmp(stringone, stringtwo)
Argument | Argument Meaning |
---|---|
stringone | Mandatory. Specifies the first string to compare |
stringtwo | Mandatory. Specifies the second string to compare |
Code Example
1
2
3
4
5
|
<?php
echo strcmp("This is a string in PHP!", "This is a string in PHP!").' - ';
echo strcmp("Using strings in PHP is really great!", "PHP strings are fun!").' - ';
echo strcmp("Wow", "Look how easy it is to compare strings");
?>
|
0 – 1 – 1
40. strip_tags() definition: The strip_tags()
function strips a string from HTML, XML, and PHP tags.
Usage: Removes PHP and HTML tags from string and returns the result. The allow parameter canbe specified to not remove certain tags. The string should be a comma-separated list of the tags to ignore; for example, "<b>,<i>" will leave bold and italic tags.
strip_tags function signature strip_tags(string, allow)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to check |
allow | Not Mandatory. Specifies allowable tags. These tags will not be removed |
Code Example
1
2
3
4
5
6
7
8
|
<?php
$markedup = '<h2>41. substr_compare() <small><strong>definition:</strong> The <code>substr()</code> function returns a part of a string.</small></h2>';
$rawtext = strip_tags($markedup);
echo $rawtext;
?>
|
41. substr_compare() definition: The substr() function returns a part of a string.
41. substr_compare() definition: The substr_compare()
function compares two strings from a specified start position.
Usage: Compares stringone, starting at the position startpos, to stringtwo. If length is specified, a maximum of that many characters are compared. Finally, if case is specified and true, the comparison is case-insensitive. Returns a number less than 0 if the substring of stringone is less than stringtwo, 0 if they are equal, and a number greater than 0 if the substring of stringone is greater than stringtwo.substr_compare function signature substr_compare(stringone, starttwo, startpos, length, case)
Argument | Argument Meaning |
---|---|
stringone | Mandatory. Specifies the first string to compare |
stringtwo | Mandatory. Specifies the second string to compare |
startpos | Mandatory. Specifies where to start comparing in string1. If negative, it starts counting from the end of the string |
length | Not Mandatory. Specifies how much of string1 to compare |
case | Not Mandatory. A boolean value that specifies whether or not to perform a case-sensitive compare:
|
Code Example
1
2
3
4
5
6
7
8
|
<?php
$equal = substr_compare("You won't believe it, but this comparison is equal", "comparison is equal", 31);
if($equal == 0){
echo 'The substrings are equal!';
}
?>
|
The substrings are equal!
42. setlocale() definition: The setlocale()
function sets locale information which refers to language, monetary,
time and other information specific for a geographical area.
Usage: Sets the locale for constant functions to location. Returns the current location after being set, or false if the location cannot be set. Any number of options for constant can be added (or ORed) together.setlocale function signature setlocale(constant, location)
Argument | Argument Meaning |
---|---|
constant | Mandatory. Specifies what locale information should be set.
Available constants:
|
location | Mandatory. Specifies what country/region to set the locale
information to. Can be a string or an array. It is possible to pass
multiple locations.
If the location is NULL or the empty string “”, the location
names will be set from the values of environment variables with the same
names as the constants above, or from “LANG”. If the location is “0”, the location setting is not affected, only the current setting is returned. If the location is an array, setlocale() will try each array element until it finds a valid language or region code. This is very useful if a region is known under different names on different systems. |
Code Example
1
2
3
|
<?php
echo setlocale(LC_ALL, "US");
?>
|
English_United States.1252
43. str_ireplace() definition: The str_ireplace()
function replaces some characters with some other characters in a string.
Usage: Performs a case-insensitive search for all occurrences of find in string and replaces themwith replace. If all three parameters are strings, a string is returned. If string is an array, the
replacement is performed for every element in the array and an array of results is returned. If find and replace are both arrays, elements in find are replaced with the elements in replace with the same numeric indices. Finally, if find is an array and replace is a string, any occurrence of any element in find is changed to replace. If supplied, count is filled with the number of instances replaced.
str_ireplace function signature str_ireplace(find, replace, string, count)
Argument | Argument Meaning |
---|---|
find | Mandatory. Specifies the value to find |
replace | Mandatory. Specifies the value to replace the value in find |
string | Mandatory. Specifies the string to be searched |
count | Not Mandatory. A variable that counts the number of replacements |
Code Example
1
2
3
4
5
6
|
<?php
$array = array("pretty neat", "mildy interesting", "really fun", "boring as ever");
print_r(str_ireplace("pretty neat", "wicked awesome", $array, $i));
echo "We made $i replacement!";
?>
|
Array
(
[0] => wicked awesome
[1] => mildy interesting
[2] => really fun
[3] => boring as ever
)
We made 1 replacements!
(
[0] => wicked awesome
[1] => mildy interesting
[2] => really fun
[3] => boring as ever
)
We made 1 replacements!
44. strncasecmp() definition: The strncasecmp()
function compares two strings.
Usage: Compares two strings; returns a number less than 0 if stringone is less than stringtwo, 0 if the two strings are equal, and a number greater than 0 if stringone is greater than stringtwo.
The comparison is case insensitive meaning, “Alphabet” and “alphabet”
are considered equal. This function is a case insensitive version of strcmp()
. If either string is shorter than length characters, the length of that string determines how many characters are compared.strncasecmp function signature strncasecmp(stringone, stringtwo, length)
Argument | Argument Meaning |
---|---|
stringone | Mandatory. Specifies the first string to compare |
stringtwo | Mandatory. Specifies the second string to compare |
length | Mandatory. Specify the number of characters from each string to be used in the comparison |
Code Example
1
2
3
4
5
|
<?php
echo strncasecmp("Awesome", "Awesome",8);
echo "<br>";
echo strncasecmp("Awesome", "aWeSoMe",8);
?>
|
0
0
0
45. crypt() definition: The crypt()
function returns a string encrypted using DES, Blowfish, or MD5 algorithms.
Usage: Encrypts str using the DES encryption algorithm seeded with the two-character salt value salt. If salt is not supplied, a random salt value is generated the first time crypt()
is calledin a script; this value is used on subsequent calls to
crypt()
. Returns the encrypted string.crypt function signature crypt(str, salt)
Argument | Argument Meaning |
---|---|
str | Mandatory. Specifies the string to be encoded |
salt | Not Mandatory. A string used to increase the number of characters encoded, to make the encoding more secure. If the salt argument is not provided, one will be randomly generated by PHP each time you call this function. |
Code Example
1
2
3
4
5
|
<?php
$secret = crypt('Password123', '$2a$10$1qAz2wSx3eDc4rFv5tGb5t');
echo "Can you guess what the characters of my password are? You can use $secret for a hint.";
?>
|
Can you guess what the
characters of my password are? You can use
$2a$10$1qAz2wSx3eDc4rFv5tGb5eByw4KQDygkPgOmhrh8p0Ix50i4Pujwm for a hint.
46. printf() definition: The printf()
function outputs a formatted string.
Usage: Outputs a string created by using format
and the given arguments. The arguments are placed into the string in
various places denoted by special markers in the format string. Each
marker starts with a percent sign (%) and consists of the following
elements, in order. Except for the type specifier, the specifiers are
all optional. To include a percent sign in the string, use %%.printf function signature printf(format, arg1, arg2, arg++)
Argument | Argument Meaning |
---|---|
format | Mandatory. Specifies the string and how to format the variables in it.
Possible format values:
|
arg1 | Mandatory. The argument to be inserted at the first %-sign in the format string |
arg2 | Not Mandatory. The argument to be inserted at the second %-sign in the format string |
arg++ | Not Mandatory. The argument to be inserted at the third, fourth, etc. %-sign in the format string |
Code Example
1
2
3
4
5
6
7
8
|
<?php
$fahrenheit = 68;
$celsius = ($fahrenheit - 32) * 5 / 9;
printf("%.2fF is %.2fC", $fahrenheit, $celsius);
?>
|
68.00F is 20.00C
47. quoted_printable_decode() definition: The quoted_printable_decode()
function decodes a quoted-printable string to an 8-bit ASCII string.
Usage: Decodes string, which is data encoded using the quoted printable encoding, and returns the resulting string.quoted_printable_decode function signature quoted_printable_decode(string)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the quoted-printable string to be decoded |
Code Example
1
2
3
4
|
<?php
$str = "Behold=0Athe=0AQuoted=0APrintable=0ADecode.";
echo quoted_printable_decode($str);
?>
|
Behold
the
Quoted
Printable
Decode.
the
Quoted
Printable
Decode.
48. sscanf() definition: The sscanf()
function parses input from a string according to a specified format.
Usage: The sscanf()
function decomposes a string according to a printf()
– like templatesscanf function signature sscanf(string, format, arg1, arg2, arg++)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to read |
format | Mandatory. Specifies the format to use.
Possible format values:
|
arg1 | Not Mandatory. The first variable to store data in |
arg2 | Not Mandatory. The second variable to store data in |
arg++ | Not Mandatory. The third, fourth, and so on, to store data in |
Code Example
1
2
3
4
5
|
<?php
$string = "LenovotUltrabook (400)";
$a = sscanf($string, "%st%s (%d)");
print_r($a);
?>
|
Array
(
[0] => Lenovo
[1] => Ultrabook
[2] => 400
)
(
[0] => Lenovo
[1] => Ultrabook
[2] => 400
)
49. crc32() definition: The crc32()
function calculates a 32-bit CRC (cyclic redundancy checksum) for a string.
Usage: Calculates and returns the cyclic redundancy checksum (CRC) for string.crc32 function signature crc32(string)
Argument | Argument Meaning |
---|---|
string | Mandatory. The string to be calculated |
Code Example
1
2
3
4
|
<?php
$str = crc32("Eat Your Leafy Greens!");
printf("%un",$str);
?>
|
2184231086
50. htmlentities() definition: The htmlentities()
function converts characters to HTML entities.
Usage: Converts all characters in string
that have special meaning in HTML and returns the resulting string. All
entities defined in the HTML standard are converted. If supplied, flags
determines the manner in which quotes are translated.htmlentities function signature htmlentities(string, flags, character-set, double_encode)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to convert |
flags | Not Mandatory. Specifies how to handle quotes, invalid encoding and the used document type.
The available quote styles are:
|
character-set | Not Mandatory. A string that specifies which character-set to use.
Allowed values are:
|
double_encode | Not Mandatory. A boolean value that specifies whether to encode existing html entities or not.
|
Code Example
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php
$url = array(
'value' => urlencode('awesome')
);
$link = "http://vegibit.com/themes/vegitalian/index.php?var={$url['value']}";
$html = array(
'link' => htmlentities($link, ENT_QUOTES, 'UTF-8')
);
echo "<a href="{$html['link']}">Bootstrap Themes!</a>";
?>
|
51. levenshtein() definition: The levenshtein()
function returns the Levenshtein distance between two strings.
Usage:
Calculates the Levenshtein distance between two strings. This is the
number of characters you have to replace, insert, or delete to transform
stringone into stringtwo. By default, replacements, inserts, and deletes have the same cost, but you can specify different costs with insert, replace, and delete.levenshtein function signature levenshtein(stringone, stringtwo, insert, replace, delete)
Argument | Argument Meaning |
---|---|
stringone | Mandatory. First string to compare |
stringtwo | Mandatory. Second string to compare |
insert | Not Mandatory. The cost of inserting a character. Default is 1 |
replace | Not Mandatory. The cost of replacing a character. Default is 1 |
delete | Not Mandatory. The cost of deleting a character. Default is 1 |
Code Example
1
2
3
4
|
<?php
$needtochange = levenshtein("Avenues", "Awesome");
echo "Avenues to Awesome has a Levenshtein distance of $needtochange";
?>
|
Avenues to Awesome has a Levenshtein distance of 5
52. strcasecmp() definition: The strcasecmp()
function compares two strings.
Usage: Compares two strings; returns a number less than 0 if stringone is less than stringtwo, 0 if the two strings are equal, and a number greater than 0 if stringone is greater than stringtwo. The comparison is case insensitive meaning, “Alphabet” and “alphabet” are considered equal.strcasecmp function signature strcasecmp(stringone, stringtwo)
Argument | Argument Meaning |
---|---|
stringone | Mandatory. Specifies the first string to compare |
stringtwo | Mandatory. Specifies the second string to compare |
Code Example
1
2
3
4
5
|
<?php
echo strcasecmp("Awesome", "Awesome");
echo "<br>";
echo strcasecmp("AwEsOmE", "awesome");
?>
|
0
0
0
53. strcspn() definition: The strcspn()
function returns the number of characters (including whitespaces)
found in a string before any part of the specified characters are
found.
Usage: Returns the length of the subset of string starting at start, examining a maximum of length characters, to the first instance of a character from char.strcspn function signature strcspn(string, char, start, length)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to search |
char | Mandatory. Specifies the characters to search for |
start | Not Mandatory. Specifies where in string to start |
length | Not Mandatory. Specifies the length of the string (how much of the string to search) |
Code Example
1
2
3
4
|
<?php
$letters = strcspn("Solid State Storage!", "!");
echo "There are $letters letters before the exclamation point.";
?>
|
There are 19 letters before the exclamation point.
54. strripos() definition: The strripos()
function finds the position of the last occurrence of a string inside another string.
Usage: Returns the position of the last occurrence of find in string using a case-insensitive search,or false if find is not found. If specified and positive, the search begins start characters
from the start of string. If specified and negative, the search begins start characters from
the end of string. This function is a case-insensitive version of
strrpos()
.strripos function signature strripos(string, find, start)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to search |
find | Mandatory. Specifies the string to find |
start | Not Mandatory. Specifies where to begin the search |
Code Example
1
2
3
4
5
6
7
|
<?php
$scooby = "Scooby Dooby Doo, Where are you Sooby Doo?";
$position = strripos("$scooby","doo");
echo "The last occurrence of 'doo' is at position $position."
?>
|
The last occurrence of ‘doo’ is at position 38.
55. strspn() definition: The strspn()
function returns the number of characters found in the string that contains only characters from the charlist parameter.
Usage: Returns the length of the substring in string that consists solely of characters in charlist. If start is positive, the search starts at that character; if it is negative, the substring starts at the character start characters from the string’s end. If length is given and is positive, that many characters from the start of the substring are checked. If length is given and is negative, the check ends length characters from the end of string.strspn function signature strspn(string, charlist, start, length)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to search |
charlist | Mandatory. Specifies the characters to find |
start | Not Mandatory. Specifies where in the string to start |
length | Not Mandatory. Defines the length of the string |
Code Example
1
2
3
4
5
6
|
<?php
$number = 777;
$isValidNumber = strspn($number, "1234567890") == strlen($number);
echo $isValidNumber;
?>
|
1
56. html_entity_decode() definition: The html_entity_decode()
function converts HTML entities to characters.
Usage: Converts all HTML entities in string to the equivalent character. All entities defined in the HTML standard are converted. If supplied, flags determines the manner in which quotes are translated. The possible values for flags are the same as those for htmlentities. If supplied, character-set determines the final encoding for the characters. The possible values for character-set are the same as those for htmlentities.html_entity_decode function signature html_entity_decode(string, flags, character-set)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to decode |
flags | Not Mandatory. Specifies how to handle quotes and which document type to use.
The available quote styles are:
|
character-set | Not Mandatory. A string that specifies which character-set to use.
Allowed values are:
|
Code Example
1
2
3
4
|
<?php
$str = "<h1>©®™</h1>";
echo html_entity_decode($str);
?>
|
©®™
57. str_shuffle() definition: The str_shuffle()
function randomly shuffles all the characters of a string.
Usage: Rearranges the characters in string into a random order and returns the resulting string.str_shuffle function signature str_shuffle(string)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to shuffle |
Code Example
1
|
earics.mh apm soi eas gsefsT
|
echo str_shuffle("This is a message from space.");
?>
58. stristr() definition: The stristr()
searches for the first occurrence of a string inside another string.
Usage: This is the case insensitive version of strstr()
.stristr function signature stristr(string, search, before_search)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to search |
search | Mandatory. Specifies the string to search for. If this parameter is a number, it will search for the character matching the ASCII value of the number |
before_search | Not Mandatory. A boolean value whose default is “false”. If set to “true”, it returns the part of the string before the first occurrence of the search parameter. |
Code Example
1
2
3
4
5
|
<?php
$sentence = "PHP is a great open source software community.";
echo stristr($sentence, 'GrEaT');
?>
|
great open source software community.
59. strpbrk() definition: The strpbrk()
function searches a string for any of the specified characters.
Usage: Returns a string consisting of the substring of string, starting from the position of the first instance of a character from charlist in string to the end of the string, or false if none of the characters in charlist is found in string.strpbrk function signature strpbrk(string, charlist)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to search |
charlist | Mandatory. Specifies the characters to find |
Code Example
1
2
3
|
<?php
echo strpbrk("Jumping Jack Flash is a Gas!"," ");
?>
|
Jack Flash is a Gas!
60. strrev() definition: The strrev()
function reverses a string.
Usage: If you need to know how to spell something backwards, this is the function for you..strrev function signature strrev(string)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to reverse. |
Code Example
1
2
3
4
5
|
<?php
$reverse = strrev('Awesome');
echo "Awesome spelled backwards is $reverse";
?>
|
Awesome spelled backwards is emosewA
61. addslashes() definition: The addslashes()
function returns a string with backslashes in front of predefined characters.
Usage: Returns
escaped instances of characters in string by adding a backslash before
them. You can specify ranges of characters by separating them with two
periods; for example, to escape characters between b and o, use "b..o".
Multiple characters and ranges can be specified in characters.addslashes function signature addslashes(string)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to be escaped. |
Code Example
1
2
3
4
|
<?php
$str = addslashes('Tim said, "This is the best stuff so far"');
echo($str);
?>
|
Tim said, “This is the best stuff so far”
62. count_chars() definition: The count_chars()
function returns information about characters used in a string
Usage: Returns the number of occurrences of each byte value from 0–255 in string – mode determines the form of the result.count_chars function signature count_chars(string, mode)
Argument | Argument Meaning |
---|---|
string | Required. The string to be checked |
mode | Not Mandatory. Specifies the return modes. 0 is default. The different return modes are:
|
Code Example
1
2
3
4
|
<?php
$str = "How many times per week do you eat at Chipotle Mexican Grill?";
echo count_chars($str, 3);
?>
|
?CGHMacdehiklmnoprstuwxy
63. md5_file() definition: The md5_file()
function calculates the MD5 hash of a file.
Usage: Calculates and returns the MD5 encryption hash for the file at file. An MD5 hash is a 32-character hexadecimal value that can be used to checksum a file’s data. If raw is supplied and is true, the result is sent as a 16-bit binary value instead.md5_file function signature substr(file, raw)
Argument | Argument Meaning |
---|---|
file | Mandatory. The file to be calculated |
raw | Not Mandatory. A boolean value that specifies hex or binary output format:
|
Code Example
1
2
3
4
5
|
<?php
$file = "somefile.txt";
$md5file = md5_file($file);
echo $md5file;
?>
|
453753ba5c6198a03deaa724b58c3eec
64. nl2br() definition: The nl2br()
function inserts HTML line breaks (<br> or <br />) in front of each newline (n) in a string.
Usage: If xhtml is true, then nl2br will use XHTML-compatible line breaks..nl2br function signature nl2br(string, xhtml)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to check |
xhtml | Not Mandatory. A boolean value that indicates whether or not to use XHTML compatible line breaks:
|
Code Example
1
2
3
|
<?php
echo nl2br("This is a piece of text on line 1.nYet this text is on line 2.");
?>
|
This is a piece of text on line 1.
Yet this text is on line 2.
Yet this text is on line 2.
65. sha1_file() definition: The sha1_file()
function calculates the SHA-1 hash of a file.
Usage: Calculates and returns the sha1 encryption hash for the file at file. A sha1 hash is a 40-character hexadecimal value that can be used to checksum a file’s data. If raw is supplied and is true, the result is sent as a 20-bit binary value instead.sha1_file function signature sha1_file(file, raw)
Argument | Argument Meaning |
---|---|
file | Mandatory. The file to be calculated |
raw | Not Mandatory. A boolean value that specifies hex or binary output format:
|
Code Example
1
2
3
4
5
|
<?php
$file = "somefile.txt";
$sha1file = sha1_file($file);
echo $sha1file;
?>
|
f9515988673aae7dc5552d24c06ce947684c6bbb
66. str_word_count() definition: The str_word_count()
function returns a part of a string.
Usage: Counts the number of words in string using locale-specific rules. The value of return dictates the returned value.str_word_count function signature str_word_count(string, return, char)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to check |
return | Not Mandatory. Specifies the return value of the str_word_count() function.
Possible values:
|
char | Not Mandatory. Specifies special characters to be considered as words. |
Code Example
1
2
3
4
5
|
<?php
$words = str_word_count("Super Mario is all you need to have a blast!");
echo "There are $words words in the sentence 'Super Mario is all you need to have a blast!"
?>
|
There are 10 words in the sentence ‘Super Mario is all you need to have a blast!
67. strnatcasecmp() definition: The strnatcasecmp()
function compares two strings using a natural order algorithm.
Usage: Compares two strings with case insensitivity and returns a number less than 0 if stringone is less than stringtwo, 0 if the two strings are equal, and a number greater than 0 if stringone is greater than stringtwo.strnatcasecmp function signature strnatcasecmp(stringone, stringtwo)
Argument | Argument Meaning |
---|---|
stringone | Mandatory. Specifies the first string to compare |
stringtwo | Mandatory. Specifies the second string to compare |
Code Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?php
$firstarray = $secondarray = array(
"file1",
"file2",
"file10",
"file01",
"file100",
"file20",
"file30",
"file200"
);
echo "This sort uses standard ordering" . "<br />";
usort($firstarray, "strcmp");
print_r($firstarray);
echo "<br />";
echo "This uses the Natural Order" . "<br />";
usort($secondarray, "strnatcmp");
print_r($secondarray);
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
This sort uses standard ordering
Array
(
[0] => file01
[1] => file1
[2] => file10
[3] => file100
[4] => file2
[5] => file20
[6] => file200
[7] => file30
)
This uses the Natural Order
Array
(
[0] => file01
[1] => file1
[2] => file2
[3] => file10
[4] => file20
[5] => file30
[6] => file100
[7] => file200
)
|
68. strnatcmp() definition: The strnatcmp()
function compares two strings using a natural order algorithm.
Usage: Compares two strings with case sensitivity and returns a number less than 0 if stringone is less than stringtwo, 0 if the two strings are equal, and a number greater than 0 if stringone is greater than stringtwo.strnatcmp function signature strnatcmp(string,start,length)
Argument | Argument Meaning |
---|---|
stringone | Mandatory. Specifies the first string to compare |
stringtwo | Mandatory. Specifies the second string to compare |
Code Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?php
$firstarray = $secondarray = array(
"FilE1",
"fiLE2",
"filE10",
"fiLE01",
"fILe100",
"fIle20",
"File30",
"FIle200"
);
echo "This sort uses standard ordering" . "<br />";
usort($firstarray, "strcmp");
print_r($firstarray);
echo "<br />";
echo "This uses the Natural Order" . "<br />";
usort($secondarray, "strnatcmp");
print_r($secondarray);
?>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
This sort uses standard ordering
Array
(
[0] => FIle200
[1] => FilE1
[2] => File30
[3] => fILe100
[4] => fIle20
[5] => fiLE01
[6] => fiLE2
[7] => filE10
)
This uses the Natural Order
Array
(
[0] => FIle200
[1] => FilE1
[2] => File30
[3] => fILe100
[4] => fIle20
[5] => fiLE01
[6] => fiLE2
[7] => filE10
)
|
69. strncmp() definition: The strncmp()
function compares two strings.
Usage: This function works just like strcmp()
function, except that strcmp()
does not have the length parameter.strncmp function signature strncmp(stringone, stringtwo, length)
Argument | Argument Meaning |
---|---|
stringone | Mandatory. Specifies the first string to compare |
stringtwo | Mandatory. Specifies the second string to compare |
length | Mandatory. Specify the number of characters from each string to be used in the comparison |
Code Example
1
2
3
4
5
6
7
8
|
<?php
$equal = strncmp("Awesome people do awesome things.", "Awesome birds fly fast.", 7);
$notequal = strncmp("Awesome people do awesome things.", "Awesome birds fly fast.", 9);
echo "Since $equal is equal, the value is $equal. Since $notequal is not equal, the value is $notequal.";
?>
|
Since $equal is equal, the value is 0. Since $notequal is not equal, the value is 1.
70. chop() definition: The chop()
function removes whitespaces or other predefined characters from the right end of a string.
Usage: This works just like the rtrim()
function.chop function signature chop(string, charlist)
Argument | Argument Meaning |
---|---|
string | Mandatory. Specifies the string to check |
charlist | Not Mandatory. Specifies which characters to remove from the string. The following characters are removed if the charlist parameter is empty:
|
Code Example
1
2
3
4
|
<?php
$chopped = rtrim('You really are the most interesting person in the world. cANyOUbELIEVEtHAT?', 'cANyOUbELIEVEtHAT?');
echo $chopped;
?>
|
You really are the most interesting person in the world.
0 comments:
Post a Comment