PHP String Functions

In our previous tutorial, we have learnt about how to specify string in PHP. In this tutorial, we will learn about the basic string functions of PHP.

PHP provides number of built-in functions that are used to manipulate strings data.

Basic PHP Strings Function

In the following section, we are going to discuss basic PHP string functions which are most commonly used in PHP script.

1) PHP strlen() function

PHP has a built-in function to check the length of the string. This is the most commonly used string function to validate the input field where users provide some characters of a certain length.

Syntax

int strlen ( string $string )

Example

<?php 
$str="Welcome to Tutorialsbook!"; 
$str=strlen($str); 
echo $str; 
?>

Output

25

2) PHP str_word_count() function

This function enables to count the numbers of words in the given string. This function is also used to validate the input fields.

Syntax 

string str_word_count ( string $string )

Example

<?php
$str="Welcome to Tutorialsbook!";
$str=str_word_count($str);
echo $str;
?> 

Output

3

3) PHP strrev() function

This function reverse the given string. You can use this function to get the reverse the your string.

Syntax

string strrev ( string $string )

Example

<?php
$str="Welcome to Tutorialsbook!";
$str=strrev($str);
echo $str;
?>

Output

!koobslairotuT ot emocleW

4) PHP strtolower() function

The PHP strtolower() function returns string in lowercase letters.

Syntax

string strtolower ( string $string )

Example

 <?php 
$str="Welcome to Tutorialsbook!"; 
$str=strtolower($str); 
echo $str; 
?>

Output

welcome to tutorialsbook!

5) PHP strtoupper() function

The PHP strtoupper() function returns string in uppercase letter.

Syntax

string strtoupper ( string $string )

Example

<?php 
$str="Welcome to Tutorialsbook!"; 
$str=strtoupper($str); 
echo $str; 
?>

Output

WELCOME TO TUTORIALSBOOK!

6) PHP ucfirst() function

This string function returns the uppercase of the first letter of the given string. Ther is no effect on the other letters of the string.

Syntax

string ucfirst ( string $string )

Example

<?php 
$str="welcome to Tutorialsbook!"; 
$str=ucfirst ($str); 
echo $str; 
?>

Output

Welcome to Tutorialsbook!

7) PHP lcfirst() function

The lcfirst() function returns the lowercase letter only for the first letter of the given string. Ther is no effect on the other letters of the string.

Syntax

string lcfirst ( string $string )

Example

<?php 
$str="Welcome to Tutorialsbook!"; 
$str=lcfirst ($str); 
echo $str; 
?>

Output

welcome to Tutorialsbook!

8) PHP ucwords() function

The PHP ucwords() converts the each letter of the words into uppercase for the given string. Ther is no effect on the other letters of the string.

Syntax

string ucwords ( string $string )

Example

<?php 
$str="welcome to Tutorialsbook!"; 
$str=ucwords ($str); 
echo $str; 
?>

Output

Welcome To Tutorialsbook!

9) PHP substr() function

Syntax

string substr ( string $string, start, length )

Example

<?php 
$str="welcome to Tutorialsbook!"; 
$str1=substr ($str,8); 
$str2=substr ($str,0,10); 
echo "$str1 <br/> $str2"; 
?>

Output

to Tutorialsbook!
welcome to

Please get connected & share!

Advertisement