String functions in PHP

Series of functions that we discussed in this session is the String functions , which are used to process the string data type or give the appearance of a certain format . Some string functions which we will discuss are:
Echo function ( ) and Print ( )
This function is a function that is very frequently found in PHP , because this function is used to display a string or text to the browser . The syntax is as follows :
Echo (string )
Print (string )
Surely you are familiar about the use of these functions , because in previous articles has been very often used as an example.
The function printf () and Sprint ( )
Both these functions is used to display output to the browser with a specific format . The syntax is as follows :
Printf (format [, argument ] )
Sprintf (format [, argument ] )
Parameter format is always marked with characters percent ( % ) , followed by certain characters that provide specifications to provide results with a specific format . Character giver specifications are:
Character Information
bThe argument is treated as an integer , and displayed as a binary number.
cThe argument is treated as an integer , and displayed as a character with a value ASCIInya .
dThe argument is treated as an integer , and displayed as a decimal number .
fThe argument is treated as a double , and displayed as floating point numbers .
oThe argument is treated as an integer , and displayed as octal numbers .
sThe argument is treated and displayed as a string .
xThe argument is treated as an integer and displayed as a hexadecimal number ( with lowercase) .
XThe argument is treated as an integer and displayed as hexadecimal numbers ( with capital letters ) .
Example :

$ angka1 = 68.75 ;
$ angka2 = 54.35 ;
$ number = $ angka1 + $ angka2 ;
/ / echo $ number will produce " 123.1 ";
$ format = sprintf ( "% 01.2f " , $ number) ;
/ / echo $ format will result in " 123.10 "
Among the characters % and character of the giver specifications can also be inserted figures. Figures in front of the character of the giver specification indicates the number of digits or the number of characters (minimum ) are shown. If there is a point number , meaning it shows the number of decimal places . For example "% 01.2f "indicates that the numbers are displayed as floating point and there should be two decimal places displayed , "% 02d "indicates that the number is displayed as an integer and there should be two digits are displayed , so if there are only eight digits , will be changed to 2008 .
Example :
$ year = 1982 ;
$ month = 2 ;
$ day = 27 ;
$ date = sprintf ( "% 04d -% 02d -% 02d " , $ year , $ month , $ day) ;
/ / echo $ date will result in " 1982-02-27 "

Htmlentities function ( )
This function is used to stop the process penerjemaahan HTML tags by the browser, so HTML tags will be allowed to appear as it is, without translation by the browser . The syntax is as follows :
Htmlentities (string )
Example :
$ str = " Click Here < / a> " ;
$ result = htmlentities ( $ str ) ;
echo "$ str ";
echo "$ result ";
?>
If the variable $ str directly subjected to function echo () , then what emerges is a link that reads " Click Here " , but if subjected to htmlentities ( ) first as shown by the variable $ result , then what emerges is " Click Here < / a > " .
Things like this is useful for example if we want to show the use of HTML tutorials in the form of HTML files as well. As examples, for instance we want to teach how to make a link . If you want to write " Click Here < / a > "without translated by the browser , then we should write it as follows :
Click Here < / a >
Obviously a very difficult and time consuming is not it? Htmlentities () will facilitate our work.
Strstr function ( ) , Stristr ( ) , and Strchr ( )
The third function is used to find the existence of a string within another string. The syntax is as follows :

Strstr ( destination , search )
Stristr ( destination , search )
Strchr ( destination , search )
Of the three functions, the only stristr () which is not case sensitive . Parameter is the goal sought by the search string . The results obtained are starting from the first search character is found until the end of the destination string . If the search string is not contained in string destination , then the function will produce a value of False.
Example :
$ Email = " user@domain.comThis e - mail address is being protected from spambots . You need JavaScript enabled to view it ";
$ Domain = strstr ( $ email ,'@');
echo $ domain ;
will generate "@ domain.com "

Strlen function ( )
This function is used to measure the length of a string of characters . The syntax is as follows :

Strlen (string )
A space will be counted as a character .
Example :
$ str = "Test the number of variables ";
$ PJ = strlen ( $ str ) ;
/ / variables $ PJ worth 18
Strrev function ( )
This function is used to reverse the order of the characters string compiler from front to rear to be from back to front . In other words, this function is used to read the string in reverse .
Strrev (string )
Example :
strrev echo ( $ str ) ;
/ / result is " PHP UTI hadum rajaleB " ? >
Function str_replace ( )
This function is used to replace a string with another string . The syntax is as follows :
Str_replace ( yang_diganti , substitutes , goal )
Parameter yang_diganti indicates a string that will be replaced .
Parameter substitute indicates a string that will replace it.
Parameter goal is the overall string containing string in which yang_diganti. Can also refer to a particular variable that contains a string .
Example :
echo str_replace ( " difficult " , " easy " , $ str ) ;
/ / result is " Learning PHP is easy " ? >
The function substr ( )
This function is used to retrieve or bypass a section of a string and displays it as a separate string . The syntax is as follows :
Substr (string , start [ , length ] )
String will be taken or withheld from the character located at the number indicated by the parameters start with the number of digits indicated by the length parameter .
Example :
$ rest = substr ( " ABCDEF " , 1 ) ; / / produces " bcdef "
$ rest = substr ( " ABCDEF " , 1 , 3 ) ; / / produces " BCD "
Start parameters can also be filled with the negative . If negative , then the calculation starts from the very back of the character .
Example :
$ rest = substr ( " ABCDEF " , -1 ) ; / / produces " f "
$ rest = substr ( " ABCDEF " , -2 ) ; / / produces " ef "
$ rest = substr ( " ABCDEF " , -3 , 1 ) ; / / produces " d "

Strtolower function ( ) and Strtoupper ( )

Both these functions are used to convert a string into all uppercase or all lowercase letters . The syntax is as follows :
Strtolower (string )
Strtoupper (string )
Example :
$ str = "string consists of upper and lower case ";
echo strtolower ( $ str ) ;
/ / result is : " This string consists of upper and lower case "
echo strtoupper ( $ str ) ;
/ / result is : " STRING consists of CAPITALS AND SMALL "
Strpos function ( )
This function is used to determine the position of a string within another string . The syntax is as follows :
Strpos ( destination , search )
If the destination string , there are more than one character by character looking for a search , found the first characters will be used.
Example :
$ str = " bambang ";
$ search = strpos ( $ str , " b " ) ;
/ / echo $ search will yield 0
Note that it is very easy to avoid confusion between the characters found in the position to zero with a character not found. Therefore we need a way to distinguish , as follows :
$ Pos = strpos ( $ str , " b " ) ;
if ( $ pos === false) { / / note the number of signs = no 3
echo " Not found ";
}

/ / in PHP prior to 4.0b3 :
$ Pos = strpos ( $ str , " b " ) ;
if ( is_string ( $ pos ) & & ! $ pos ) {
echo " Not found "; ? >
Perhaps you are confused , what does all these functions ? Indeed as explained by these functions seems to play only for strings only, but later if the discussion we have to go deeper and begin to build a web application , these functions will appear to its usefulness . But it is not possible to build a web application without learning first base , instead : -p ? , good luck .

0 komentar:

Post a Comment