PHP Tutorials : Strings (Part 2)
In this PHP Tutorial you will learn about Strings (2nd Part) – String cleanup, String replacement, Case functions, String formatting and A list of the available type specifiers.
String cleanup:
String cleanup is removing \n, \r, \t, \0, and spaces from any side of the string, all cleanup functions take one argument, which is the string to be cleaned, and returns a new value, which is the cleaned string.
To clean up the end of the string use the function rtrim() or chop().
To clean up the start of the string use the function ltrim().
To clean both side of the string, use the function trim().
Example:
<?php
$s = “ This is a string ”;
$s1 = ltrim($s);
echo $s1;
$s2 = chop($s);
echo $s2;
$s3 = trim($s);
echo $s3;
?>
This will print:
‘This is a string‘
‘This is a string ‘
‘This is a string’
String replacement:
To replace a portion of a string, use the function str_replace().
It takes three arguments, the string to search for, the replacement string, and the string to search in.
It returns a string with all instances of the first argument are replaced by the second argument.
Example:
<?php
$s1 = “I live at Los Angeles”;
$s2 = str_replace(‘Los Angeles’, ‘San Francisco’, $s1);
echo $s2;
?>
This will print ‘I live at San Francisco’
Case functions:
• The function strtoupper() takes a string as an argument and returns the uppercase of that string.
• The function strtolower() takes a string as an argument and returns the lowercase of the string.
Example:
<?php
$s = “String”;
$s1 = strtoupper($s);
echo $s1;
$s2 = strtolower($s);
echo $s2;
?>
This will print:
STRING
string
String formatting:
To format a string, use the function printf().
A conversion specification begins with a percent (%) symbol and defines how to treat the corresponding argument to printf(). You can include as many conversion specifications as you want within the format control string, as long as you send an equivalent number of arguments to printf().
Example:
<?php
printf(“First number: %d<br>\nSecond number: %d<br>\n”, 55, 66);
?>
This will print:
First number: 55
Second number: 66
The first conversion specification corresponds to the first of the additional arguments to printf(), which is 55. The second conversion specification corresponds to 66. The d following the percent symbol requires that the data be treated as a decimal integer. This part of a conversion specification is a type specifier.
A list of the available type specifiers:
b: Display an integer as a binary number.
c: Display an integer as ASCII equivalent.
d: Display argument as a decimal number.
f: Display an integer as a floating-point number (double)
o: Display an integer as an octal number (base 8)
s: Display argument as a string
x: Display an integer as a lowercase hexadecimal number (base 16)
X: Display an integer as an uppercase hexadecimal number (base 16)
Example:
<?php
$red = 255;
$green = 255;
$blue = 255;
printf( "#%X%X%X", $red, $green, $blue );
?>
This will print "#FFFFFF"
The precision identifier controls the precision in floating-point format, it should be placed directly before the type specifier. It consists of a dot (.) followed by the number of decimal places to which you want to round. This specifier has an effect only on data that is output with the f type specifier
Example:
<?php
printf( "%.2f", 5.333333 );
?>
This will print 5.33