9. String Functions
9. String Functions
chr() Function
• The chr() function returns a character from
the specified ASCII value.
• The ASCII value can be specified in decimal,
octal, or hex values. Octal values are defined
by a leading 0, while hex values are defined by
a leading 0x.
Example
• <?php
echo chr(52) . "<br>"; // Decimal value
echo chr(052) . "<br>"; // Octal value
echo chr(0x52) . "<br>"; // Hex value
?>
ord() Function
• The ord() function returns the ASCII value of
the first character of a string.
Example
• <?php
echo ord("h")."<br>";
echo ord("hello")."<br>";
?>
strtolower() Function
• The strtolower() function converts a string to
lowercase.
Example
• <?php
echo strtolower("Hello WORLD.");
?>
strtoupper() Function
• The strtoupper() function converts a string to
uppercase.
Example
• <?php
echo strtoupper("Hello WORLD!");
?>
strlen() Function
• The strlen() function returns the length of a
string.
Example
• <?php
echo strlen("Hello");
?>
ltrim() Function
• The ltrim() function removes whitespace or
other predefined characters from the left side
of a string.
Example
• <?php
$str = "Hello World!";
echo $str . "<br>";
echo ltrim($str,"Hello");
?>
rtrim() Function
• The rtrim() function removes whitespace or
other predefined characters from the right
side of a string.
Example
• <?php
$str = "Hello World!";
echo $str . "<br>";
echo rtrim($str,"World!");
?>
trim() Function
• The trim() function removes whitespace and
other predefined characters from both sides
of a string.
Example
• <?php
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hed!");
?>
substr() Function
• The substr() function returns a part of a string.
Example
• <?php
echo substr("Hello world",6,2);
?>
– Syntax :
• substr(string,start,length)
strcmp() Function
• The strcmp() function compares two strings.