Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

PHP_String_Functions

The document provides an overview of various PHP string functions used for string manipulation, including examples for each function. Key functions discussed include echo, strtolower, strtoupper, lcfirst, ucfirst, ucwords, substr_replace, substr_count, strstr, strpos, ltrim, and rtrim. Each function is explained with syntax and output examples to illustrate its usage.

Uploaded by

qedimeliyevaa05
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

PHP_String_Functions

The document provides an overview of various PHP string functions used for string manipulation, including examples for each function. Key functions discussed include echo, strtolower, strtoupper, lcfirst, ucfirst, ucwords, substr_replace, substr_count, strstr, strpos, ltrim, and rtrim. Each function is explained with syntax and output examples to illustrate its usage.

Uploaded by

qedimeliyevaa05
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

PHP String Functions

PHP string function helps us to manipulate


string in various ways.

There are various types of string function


available.

Here we discuss some important functions and


its use with examples.

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.

The syntax is:


Substr_replace (string, replacement, start ,
length)

There are various way to replace string. Here the


examples are –
Example:
<!DOCTYPE html>
<html>
<head>
<title>string function</title>
</head>
<body>
<?php
echo substr_replace("hello world","nandon",6);
?>
</body>
</html>
Output:
hello nandon
//here it replace world to nandon . Number 6
means the replacement starts from 6 position of
the string.
Example 2:
<!DOCTYPE html>
<html>
<head>
<title>string function</title>
</head>
<body>
<?php
echo substr_replace("hello world", "nandon",-5);
?>
</body>
</html>
Output:
hello nandon
//here in this example we use (-) negative sign so it
will count the word from the right hand side and
replace.
7. substr_count( ): It counts the number of
times a substring occurs in a string.

The syntax is:


substr_count(string,substring,start,length)

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:

The syntax is:


strstr(string,search,before_search)
*Note stristr() is case insenstive
<!DOCTYPE html>
<html>
<body>

<?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:

The syntax is:


strrpos(string,find,start)
*Note stripos() is case insenstive

<!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::

The syntax is:


ltrim(string,charlist)

<!DOCTYPE html>
<html>
<body>

<?php
$str = "Hello World!";
echo $str . "<br>";
echo ltrim($str,"Hello");
?>

</body>
</html>

OUTPUT: Hello World!


World!

You might also like