Sie sind auf Seite 1von 27

bin2hex() Converts a string of ASCII characters to hexadecimal values

Removes whitespace or other characters from the right end


chop()
of a string
count_chars() Returns information about characters used in a string
chunk_split() Splits a string into a series of smaller parts
explode() Breaks a string into an array
implode() Returns a string from the elements of an array
join() Alias of implode()
lcfirst() Converts the first character of a string to lowercase
Removes whitespace or other characters from the left side of
ltrim()
a string
md5() Calculates the MD5 hash of a string
ord() Returns the ASCII value of the first character of a string
parse_str() Parses a query string into variables
print() Outputs one or more strings
printf() Outputs a formatted string
Removes whitespace or other characters from the right side
rtrim()
of a string
str_repeat() Repeats a string a specified number of times
str_replace() Replaces some characters in a string (case-sensitive)
str_split() Splits a string into an array
str_word_count() Count the number of words in a string
strcasecmp() Compares two strings (case-insensitive)
Finds the first occurrence of a string inside another string
strchr() (alias of strstr())
strcmp() Compares two strings (case-sensitive)
Returns the number of characters found in a string before
strcspn()
any part of some specified characters are found

1.bin2hex()
Definition and Usage
The bin2hex() function converts a string of ASCII characters to hexadecimal
values. The string can be converted back using the pack() function.
Syntax
bin2hex(string)
Parameter Description
String Required. The string to be converted
Example
<html>
<body>
<?php
$str = "Hello world!";
echo bin2hex($str) . "<br>";
?>
</body>
</html>
Output
48656c6c6f20776f726c6421
Hello world!

2.chop()
Definition and Usage
The chop() function removes whitespaces or other predefined characters from
the right end of a string.
Syntax
chop(string,charlist)
Parameter Description
String Required. Specifies the string to check

Optional. Specifies which characters to remove from the string.


The following characters are removed if the charlist parameter
is empty:
"\0" - NULL
Charlist "\t" - tab
"\n" - new line
"\x0B" - vertical tab
"\r" - carriage return
" " - ordinary white space
Example
<?php
$str = "Hello World!\n\n";
echo $str;
echo chop($str);
?>
Output:
Hello World! Hello World!

3.Chunk_split()
Definition and Usage
The chunk_split() function splits a string into a series of smaller parts.
Note: This function does not alter the original string.
Syntax
chunk_split(string,length,end)
Parameter Description
String Required. Specifies the string to split

Optional. A number that defines the length of the chunks.


Length
Default is 76

Optional. A string that defines what to place at the end of each


End
chunk. Default is \r\n
Example1
<?php
$str = "Hello world!";
echo chunk_split($str,1,".");
?>
Output:
H.e.l.l.o. .w.o.r.l.d.!.
Example2
Split the string after each sixth character and add a "..." after each split:
<?php
$str = "Hello world!";
echo chunk_split($str,6,"...");
?>
Output:
Hello ...world!...

4.Explode
Definition and Usage
The explode() function breaks a string into an array.
Note: The "separator" parameter cannot be an empty string.
Note: This function is binary-safe.
Syntax
explode(separator,string,limit)
Parameter Description
separator Required. Specifies where to break the string

String Required. The string to split

Optional. Specifies the number of array elements to return.


Possible values:
Greater than 0 - Returns an array with a maximum of limit
Limit element(s)
Less than 0 - Returns an array except for the last -limit
elements()
0 - Returns an array with one element
Example
Using the limit parameter to return a number of array elements:
<?php
$str = 'one,two,three,four';
// zero limit
print_r(explode(',',$str,0));
// positive limit
print_r(explode(',',$str,2));
// negative limit
print_r(explode(',',$str,-1));
?>
Output:
Array ( [0] => one,two,three,four )
Array ( [0] => one [1] => two,three,four )
Array ( [0] => one [1] => two [2] => three )
Example2:
<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>
Output:
Array ( [0] => Hello [1] => world. [2] => It's [3] => a [4] => beautiful [5] =>
day. )

5.Implode
Definition and Usage
The implode() function returns a string from the elements of an array.
Syntax
implode(separator,array)
Parameter Description
Optional. Specifies what to put between the array elements.
separator
Default is "" (an empty string)

Array Required. The array to join to a string

Example
<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr)."<br>";
echo implode("+",$arr)."<br>";
echo implode("-",$arr)."<br>";
echo implode("X",$arr);*
?>
Output:
Hello World! Beautiful Day!
Hello+World!+Beautiful+Day!
Hello-World!-Beautiful-Day!
HelloXWorld!XBeautifulXDay!

6.join()
Definition and Usage
The join() function returns a string from the elements of an array.
The join() function is an alias of the implode() function.
Note: The join() function accept its parameters in either order. However, for
consistency with explode(), you should use the documented order of arguments.
Note: The separator parameter of join() is optional. However, it is
recommended to always use two parameters for backwards compatibility.
Syntax
join(separator,array)
Parameter Description
Optional. Specifies what to put between the array elements.
separator
Default is "" (an empty string)

Array Required. The array to join to a string


Example:
<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo join(" ",$arr);
?>
Output:
Hello World! Beautiful Day!

7. lcfirst()
Definition and Usage
The lcfirst() function converts the first character of a string to lowercase.
Related functions:
ucfirst() - converts the first character of a string to uppercase
ucwords() - converts the first character of each word in a string to uppercase
strtoupper() - converts a string to uppercase
strtolower() - converts a string to lowercase
Syntax
lcfirst(string)

Parameter Description

String Required. Specifies the string to convert

Example:
<?php
echo lcfirst("Hello world!");
?>
Output:
hello world!
8. ltrim
Definition and Usage
The ltrim() function removes whitespace or other predefined characters from the
left side of a string.
Related functions:
rtrim() - Removes whitespace or other predefined characters from the right side
of a string
trim() - Removes whitespace or other predefined characters from both sides of a
string
Syntax
ltrim(string,charlist)
Parameter Description
String Required. Specifies the string to check

Optional. Specifies which characters to remove from the string.


If omitted, all of the following characters are removed:
"\0" - NULL
"\t" - tab
Charlist
"\n" - new line
"\x0B" - vertical tab
"\r" - carriage return
" " - ordinary white space
Example:
<?php
$str = "Hello World!";
echo $str . "<br>";
echo ltrim($str,"Hello");
?>
Output:
Hello World!
World!
9.Ord
Definition and Usage
The ord() function returns the ASCII value of the first character of a string.
Syntax
ord(string)
Parameter Description

String Required. The string to get an ASCII value from

Example:
<?php
echo ord("h")."<br>";
echo ord("hello")."<br>";
?>
Output:
104
104

10.Parse_str()
Definition and Usage
The parse_str() function parses a query string into variables.
Syntax
parse_str(string,array)
Parameter Description
String Required. Specifies the string to parse

Optional. Specifies the name of an array to store the variables.


Array This parameter indicates that the variables will be stored in an
array.

Example:
<?php
parse_str("name=Peter&age=43",$myArray);
print_r($myArray);
?>
Output:
Array ( [name] => Peter [age] => 43 )
11.Print
Definition and Usage
The print() function outputs one or more strings.
Note: The print() function is not actually a function, so you are not required to
use parentheses with it.
Tip: The print() function is slightly slower than echo().
Syntax
print(strings)

Parameter Description

Strings Required. One or more strings to be sent to the output

Example
<?php
print "Hello world!";
?>
Output:
Hello world!

12.Printf:
Definition and Usage
The printf() function outputs a formatted string.
The arg1, arg2, ++ parameters will be inserted at percent (%) signs in the main
string. This function works "step-by-step". At the first % sign, arg1 is inserted,
at the second % sign, arg2 is inserted, etc.
Note: If there are more % signs than arguments, you must use placeholders. A
placeholder is inserted after the % sign, and consists of the argument- number
and "\$". See example two.
Tip: Related functions: sprintf(), vprintf(), vsprintf(), fprintf() and vfprintf()
Syntax
printf(format,arg1,arg2,arg++)
Parameter Description
Required. Specifies the string and how to format the variables
in it.
Possible format values:
%% - Returns a percent sign
%b - Binary number
%c - The character according to the ASCII value
%d - Signed decimal number (negative, zero or positive)
%e - Scientific notation using a lowercase (e.g. 1.2e+2)
%E - Scientific notation using a uppercase (e.g. 1.2E+2)
%u - Unsigned decimal number (equal to or greather than zero)
%f - Floating-point number (local settings aware)
%F - Floating-point number (not local settings aware)
Format %g - shorter of %e and %f
%G - shorter of %E and %f
%o - Octal number
%s - String
%x - Hexadecimal number (lowercase letters)
%X - Hexadecimal number (uppercase letters)
Additional format values. These are placed between the % and
the letter (example %.2f):
+ (Forces both + and - in front of numbers. By default, only
negative numbers are marked)
' (Specifies what to use as padding. Default is space. Must be
used together with the width specifier. Example: %'x20s (this
uses "x" as padding)
- (Left-justifies the variable value)
[0-9] (Specifies the minimum width held of to the variable
value)
.[0-9] (Specifies the number of decimal digits or maximum
string length)
Note: If multiple additional format values are used, they must
be in the same order as above.

Required. The argument to be inserted at the first %-sign in the


arg1
format string

Optional. The argument to be inserted at the second %-sign in


arg2
the format string

Optional. The argument to be inserted at the third, fourth, etc.


arg++
%-sign in the format string
Example:
<?php
$number = 9;
$str = "Beijing";
$str1="65";
printf("There are %b million bicycles in %c %s.",$number,$str1,$str);
?>
Output:
There are 9 million bicycles in A Beijing.
Example 2:
<?php
$str1 = "Hello";
$str2 = "Hello world!";
printf("[%s]<br>",$str1); // String
printf("[%8s]<br>",$str1); // Right-justifies the string with spaces
printf("[%-8s]<br>",$str1); // Left-justifies the string value with spaces
printf("[%08s]<br>",$str1); // Zero-padding
printf("[%'*8s]<br>",$str1); // Adds "*"
printf("[%8.8s]<br>",$str2); // Left-justifies the string with spaces (cuts off
characters after the specified value)
?>
Output:
[Hello]
[ Hello]
[Hello ]
[000Hello]
[***Hello]
[Hello wo]

13.rtrim()

Definition and Usage


The rtrim() function removes whitespace or other predefined characters from
the right side of a string.
Related functions:
ltrim() - Removes whitespace or other predefined characters from the left side
of a string
trim() - Removes whitespace or other predefined characters from both sides of a
string
Syntax
rtrim(string,charlist)
Parameter Description

String Required. Specifies the string to check


Optional. Specifies which characters to remove from the string.
If omitted, all of the following characters are removed:

 "\0" - NULL
 "\t" - tab
Charlist
 "\n" - new line
 "\x0B" - vertical tab
 "\r" - carriage return
 " " - ordinary white space

Example
Remove characters from the right side of a string:
<?php
$str = "Hello World!";
echo $str . "<br>";
echo rtrim($str,"World!");
?>
Output:

Hello World!
Hello

14..repeat()
Definition and Usage
The str_repeat() function repeats a string a specified number of times.
Syntax
str_repeat(string,repeat)
Parameter Description
String Required. Specifies the string to repeat
Required. Specifies the number of times the string will be
Repeat
repeated. Must be greater or equal to 0
Example:
<?php
echo str_repeat(".",13);
?>
Output:
………….

15.replace()
Definition and Usage
The str_replace() function replaces some characters with some other characters
in a string.
This function works by the following rules:
 If the string to be searched is an array, it returns an array
 If the string to be searched is an array, find and replace is
performed with every array element
 If both find and replace are arrays, and replace has fewer elements
than find, an empty string will be used as replace
 If find is an array and replace is a string, the replace string will be
used for every find value
Note: This function is case-sensitive. Use the str_ireplace() function to perform
a case-insensitive search.
Syntax
str_replace(find,replace,string,count)
Parameter Description
Find Required. Specifies the value to find

Replace Required. Specifies the value to replace the value in find


String Required. Specifies the string to be searched
Count Optional. A variable that counts the number of replacements
Example:
Replace the characters "world" in the string "Hello world!" with "Peter":
<?php
echo str_replace("world","Peter","Hello world!");
?>
Output:
Hello Peter!
Example 2:
<?php
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "<br>" . "Replacements: $i";
?>
Output:
Array ( [0] => blue [1] => pink [2] => green [3] => yellow )
Replacements: 1
Example 3:
<?php
$find = array("Hello","world");
$replace = array("php","c");
$arr = array("Hello","world","!");
print_r(str_replace($find,$replace,$arr));
?>
Example 4:
<?php
$find = array("Hello","world");
$replace = array("c");
$arr = array("Hello","world","!");
print_r(str_replace($find,$replace,$arr));
?>
Example 5:
<?php
$find = array("Hello","world");
$replace = "c";
$arr = array("Hello","world","!");
print_r(str_replace($find,$replace,$arr));
?>

16.Split()
Definition and Usage
The str_split() function splits a string into an array.
Syntax
str_split(string,length)
Parameter Description
String Required. Specifies the string to split
Optional. Specifies the length of each array element. Default is
Length
1
Example
<?php
print_r(str_split("Hello",3));
?>
Output:
Array ( [0] => Hel [1] => lo )

17.Word_count()
Definition and Usage
The str_word_count() function counts the number of words in a string.
Syntax
str_word_count(string,return,char)
Parameter Description
String Required. Specifies the string to check

Optional. Specifies the return value of the str_word_count()


function.
Possible values:
Return 0 - Default. Returns the number of words found
1 - Returns an array with the words from the string
2 - Returns an array where the key is the position of the word in
the string, and value is the actual word

Optional. Specifies special characters to be considered as


Char
words.
EXAMPLE:
<?php
$srt=str_word_count("It is a beautiful day!");
echo $srt;
?>
Output:
5
Example 1:
Without and with the char parameter:
<?php
$str1 = str_word_count("Hello world & good morning!",1);
print_r ($str1);
$str2 = str_word_count("Hello world & good morning!",1,"&");
print_r ($str2);
?>Output:
Array ( [0] => Hello [1] => world [2] => good [3] => morning )
Array ( [0] => Hello [1] => world [2] => & [3] => good [4] => morning )

Example 2:
Return an array where the key is the position of the word in the string, and value
is the actual word:
<?php
print_r(str_word_count("Hello world!",2));
?>
Output:
Array ( [0] => Hello [6] => world )

18.Strcasecmp():
Definition and Usage
The strcasecmp() function compares two strings( case-insensitive).
Syntax
strcasecmp(string1,string2)
Parameter Description
string1 Required. Specifies the first string to compare

string2 Required. Specifies the second string to compare

This function returns:


0 - if the two strings are equal
Return Value:
<0 - if string1 is less than string2
>0 - if string1 is greater than string2
Example:
<?php
echo strcasecmp("Hello world!","HELLO WORLD!"); // The two strings are
equal
echo strcasecmp("Hello world!","HELLO"); // String1 is greater than string2
echo strcasecmp("Hello world!","HELLO WORLD! HELLO!"); // String1 is
less than string2
?>
Output:
0
7
-7

19.Strchr()
Definition and Usage
The strchr() function searches for the first occurrence of a string inside another
string.
This function is an alias of the strstr() function.
Note: This function is case-sensitive. For a case-insensitive search, use stristr()
function.
Syntax
strchr(string,search,before_search);
Parameter Description
String Required. Specifies the string to search

Required. Specifies the string to search for. If this parameter is


Search a number, it will search for the character matching the ASCII
value of the number
Optional. A boolean value whose default is "false". If set to
before_search "true", it returns the part of the string before the first occurrence
of the search parameter.
Example:
Find the first occurrence of "world" inside "Hello world!" and return the rest of
the string:
<?php
echo strchr("Hello world of php!","world");
?>
Output:
world of php!
Example 2:
Return the part of the string before the first occurence of "world":
<?php
echo strchr("Hello world!","world",true);
?>
Output:
Hello

20.Strcmp()
Definition and Usage
The strcmp() function compares two strings.
Note: The strcmp() function is case-sensitive.
Syntax
strcmp(string1,string2)
Parameter Description
string1 Required. Specifies the first string to compare

string2 Required. Specifies the second string to compare


This function returns:
0 - if the two strings are equal
Return Value:
<0 - if string1 is less than string2
>0 - if string1 is greater than string2

Example:
<?php
echo strcmp("Hello","Hello");
echo "<br>";
echo strcmp("Hello","hELLo");
?>
Output:
0
-1
21. strcspn()
Definition and Usage
The strcspn() function returns the number of characters (including whitespaces)
found in a string before any part of the specified characters are found.
Syntax
strcspn(string,char,start,length)
Parameter Description
String Required. Specifies the string to search

Char Required. Specifies the characters to search for

Start Optional. Specifies where in string to start


Length Optional. Specifies the length of the string (how much of the
string to search)
Example:
<?php
echo strcspn("Hello world!","w",0,6); // The start position is 0 and the length of
the search string is 6.
?>
Output
6
Example 2
Print the number of characters found in "Hello world!" before the character "w":
<?php
echo strcspn("Hello world!","w");
?>
Output:
Hello

22. count_chars()
Definition and Usage
The count_chars() function returns information about characters used in a
string.
Syntax
count_chars(string,mode)
Parameter Description
String Required. The string to be checked

Optional. Specifies the return modes. 0 is default. The different


Mode return modes are:
0 - an array with the ASCII value as key and number of
occurrences as value
1 - an array with the ASCII value as key and number of
occurrences as value, only lists occurrences greater than zero
2 - an array with the ASCII value as key and number of
occurrences as value, only lists occurrences equal to zero are
listed
3 - a string with all the different characters used
4 - a string with all the unused characters
Example 1
Return a string with all the unused characters in "Hello World!" (mode 4):
<?php
$str = "Hello World!";
echo count_chars($str,4);
?>
Output:
"#$%&'()*+,./0123456789:;<=>?@ABCDEFGIJKLMNOPQRSTUVXYZ[\]^_`
abcfghijkmnpqstuvwxyz{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—
˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬-
®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäå
æçèéêëìíîïðñòóôõö÷øùúûüýþÿ

For mode 3:
<?php
$str = "Hello World!";
echo count_chars($str,3);
?>
Output:
!HWdelor
For mode 1
<?php
$str = "Hello World!";
print_r(count_chars($str,1));
?>
Output:
Array ( [32] => 1 [33] => 1 [72] => 1 [87] => 1 [100] => 1 [101] => 1 [108] =>
3 [111] => 2 [114] => 1 )

23.md5()
Definition and Usage
The md5() function calculates the MD5 hash of a string.
The md5() function uses the RSA Data Security, Inc. MD5 Message-Digest
Algorithm.
The MD5 Message-Digest Algorithm: "The MD5 message-digest algorithm
takes as input a message of arbitrary length and produces as output a 128-bit
"fingerprint" or "message digest" of the input. The MD5 algorithm is intended
for digital signature applications, where a large file must be "compressed" in a
secure manner before being encrypted with a private (secret) key under a
public-key cryptosystem such as RSA."
Syntax
md5(string,raw)
Parameter Description
String Required. The string to be calculated
Optional. Specifies hex or binary output format:
Raw TRUE - Raw 16 character binary format
FALSE - Default. 32 character hex number

EXAMPLE
<?php
$str = "Hello";
echo "The string: ".$str."<br>";
echo "TRUE - Raw 16 character binary format: ".md5($str, TRUE)."<br>";
echo "FALSE - 32 character hex number: ".md5($str)."<br>";
?>
Output:
The string: Hello
TRUE - Raw 16 character binary format: ‹™SÄa–¨'«øÄx
FALSE - 32 character hex number: 8b1a9953c4611296a827abf8c47804d7

Das könnte Ihnen auch gefallen