PHP_String_Functions
PHP_String_Functions
1.echo 9.
strstr()
2.strtolower ( ) 10.
stristr()
3.strtoupper ( ) 11.
stripos()
4.lcfirst() 12. strpos()
5.ucfirst() 13. ltrim()
6.ucwords() 14. rtrim()
7.substr_replace( )
1. echo: It is used to print one or more
string .Echo function is faster than print print
function.
Example:
<!DOCTYPE html>
<html>
<head>
<title>string function</title>
</head>
<body>
<?php
echo "String Function"
?>
</body>
</html>
Output:
String Function
//echo is not actually function so we are not
required to use parentheses with it.
2. strtolower: It converts a string to lower case
letter.
Example:
<!DOCTYPE html>
<html>
<head>
<title>string function</title>
</head>
<body>
<?php
echo strtolower("STRING Function") ;
?>
</body>
</html>
Output:
string function
3. strtoupper ( ): It converts a string to upper
case letter.
Example:
<!DOCTYPE html>
<html>
<head>
<title>string function</title>
</head>
<body>
<?php
echo strtoupper("String Function") ;
?>
</body>
</html>
Output:
STRING FUNCTION
4. lcfirst( ): It converts the first character of a
string to lower.
Example:
<!DOCTYPE html>
<html>
<head>
<title>string function</title>
</head>
<body>
<?php
echo lcfirst("String Function") ;
?>
</body>
</html>
Output:
string Function
5. ucfirst( ): It converts the first character of
a string to upper case.
Example:
<!DOCTYPE html>
<html>
<head>
<title>string function</title>
</head>
<body>
<?php
echo ucfirst("string function") ;
?>
</body>
</html>
Output:
String function
6. ucwords( ): It converts the first character
of every word to upper case.
Example:
<!DOCTYPE html>
<html>
<head>
<title>string function</title>
</head>
<body>
<?php
echo ucwords("string function") ;
?>
</body>
</html>
Output:
String Function
7. substr_replace( ): It replaces a part of a
string with another string.
Example:
<!DOCTYPE html>
<html>
<head>
<title>string function</title>
</head>
<body>
<?php
echo substr_count("Hello user. Have a nice
day!","a");
?>
</body>
</html>
OUTPUT: 3
8. strstr( ) vs stristr( ): Find the first occurrence of
"world" inside "Hello world!" and return the rest of the
string:
<?php
echo strstr("Hello world!","world");
?>
</body>
</html>
OUTPUT: world!
9. strpos( ) and stripos(): Find the position of the last
occurrence of "php" inside the string:
<!DOCTYPE html>
<html>
<body>
<?php
echo strpos("I love php, I love php too!",“php");
?>
</body>
</html>
OUTPUT: 7
13. ltrim( ) vs rtrim(): Remove characters from the left
or right side of a string::
<!DOCTYPE html>
<html>
<body>
<?php
$str = "Hello World!";
echo $str . "<br>";
echo ltrim($str,"Hello");
?>
</body>
</html>