PHP 5 String Functions :-

Techno PHP

String Functions

The string functions work on the string that is part of the PHP code .You do no need to any installation . I will show you how to work string functions with the help of real website form example .


Function                              Description
strlen()                               Find out the string length
Str_words_count()               How many words in a string
PHP strrev()                        Reverse the string.
str_replace()                       Replace a part of a string with other string .
strpos()                              find out the position of search string.
strcmp()                             compare two string
strtolower()                        covert capital string to lower case .
strtoupper()                       covert lower case string to upper case .


Let’s learn in practically with HTML form and real time run .

The Length of a String:-

The strlen() function is used for find out the length of string .
Let’s take a string like "Learn PHP" and apply the strlen() function .


Example

<?php
echo strlen("Learn PHP "); // outputs 9
?>
An other example with HTML FORM (text box)

Form Exmple


ENTER YOUR TEXT:
<?php if(isset($_POST["submit"])) { $name=$_POST['name']; echo "Length:"; ; echo strlen("$name"); } ?>
Run

Str_words_count() function-

The str_word_count() function is used for counts the number of words of string .


Example

<?php
echo str_word_count("Learn PHP "); // output 2
?>
An other example with HTML FORM (text box)

Form Exmple

ENTER YOUR TEXT:

$lt;?php if(isset($_POST["submit"])) { $text=$_POST['text']; echo "Word Count:"; echo str_word_count ("$text"); } ?>
Run

PHP strrev()-

The strrev() function is used to reverse the complete string .


Example

<?php
echo strrev("Hello Learn Here")// outputs ereH nrael olleh
?>
An other example with HTML FORM (text box)

Form Exmple

ENTER YOUR TEXT:

<?php if(isset($_POST["submit"])) { $text=$_POST['text']; echo "Reverse Text :"; echo strrev("$text"); } ?>
Run

PHP str_replace() function -

The PHP str_replace() function used for replaces some characters with some other characters in a string.
In the below example replaces the text "world" with "Dolly":
In bellow example we will replaces the text “Learn “ with “Study”:


Example

<?php
echo str_replace("Learn", "Study", "Learn PHP "); // outputs Study PHP !
?>
An other example with HTML FORM (text box)

Form Exmple

Enter Your Text:

Enter Word For Replace:

New Word :

<?php if(isset($_POST["submit"])) { $text=$_POST['text']; $change =$_POST['change']; $new=$_POST['new']; echo "Replaced String :"; echo str_replace("$change","$new","$text"); } ?>
Run

strpos() function :-

The PHP strpos() function used to search a text(string ) within a string .
If the text match within a string the fuction gives position of text that you are finding if not found match it gives FALSE
In the bellow example we will search “you” in the strin “How are you ?“.


Example

<?php
echo strpos("How are you ?", "You "); // outputs 9
?>
An other example with HTML FORM (text box)

Form Exmple

Enter Text Here:

Search:

<?php if(isset($_POST["submit"])) { $text=$_POST['text']; $search=$_POST['search']; echo "Position:"; echo strpos("$text","$search"); } ?>
Run

Strcmp() fuction:-

The fuction strcmp() is used for compares two string .


Example

Name :


Compare :

<?php if(isset($_POST["submit"])) { $name=$_POST['name']; $compare=$_POST['compare']; echo "Compared :"; echo strcmp("$name","$compare"); } ?>
Run

Strtolower():-

The Strtolower() is used for convert complete string to lower case .


Example

<?php
$str="TECHNO SMARTER";

$res=strtolower($str);//for lower case 
echo $res;

?//Output  techno smarter

>
An other example with HTML FORM (text box)

Form Exmple

Enter Capital String :

<?php if(isset($_POST["submit"])) { $text=$_POST['text']; echo strtolower("$text"); } ?>
Run

Strtoupper() Function -

The Strtoupper() is opposite to string to lower function that used for convert string to upper case .


Example

Enter Small String :

<?php if(isset($_POST["submit"])) { $text=$_POST['text']; echo strtoupper("$text"); } ?>
Run

Related