Please note, this is a STATIC archive of website technosmarter.com from 20 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
 

PHP String Functions


The string functions work on the string that is part of the PHP code. These string function are readymade PHP functions. You do not need any installation. These functions are used for various task. This is a major advantage if you are working on a project and want to use some real website example, you can find here. Let's understand with an example. Suppose that, you are developing word count application on PHP. To complete your word count project, you have to use string word count function.PHP included many string function. You need to understand.


String Functions                              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 .


Strlen() function

The strlen() function refers to string length function.The strlen() function is used to find out the length of the string. If you want to find the length of string , you have to use PHP strlen() function.
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

String word count function : str_word_count() -

There are a lot of word count PHP applications running on the internet. These applications are developed on PHP string word count function. The str_word_count() function stands for string word count function. Let's have an example. If you want to count paragraph words, you have to develop a word count application. You can develop word count application using string word count function

Example

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

Form Exmple

ENTER YOUR TEXT:

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

String reverse function : strrev()-

The strrev() function is known as a string reverse function. As the name indicates reverse, the function works the same on behalf of the name. The strrev() function is used to reverse the complete string. If you enter the string "Hello" and apply strrev() function. It will convert to reverse "olleh" The following example will help you understand string reverse function.


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

String replcae function : str_replace() -

The str_replace() function refers to string replace function. Str_replace() function is also readymade function of PHP.You do not need to create a function for it. The PHP str_replace() function used to replace one string to another. Let's understand with a real example. Everybody knows about notepad. The notepad is a system application. You are typing some content on a notepad. In the end, you need to replace some words. In notepad, you can replace words using find and replace function. In the PHP application, you can replace words using string replace function.
In the below example replace the string "world" with "Dolly":

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

String position function: strpos():-

The strpos() function is known as a string position function. It's used to find out the string position. If you have written a paragraph and want to find out a word position within a paragraph then you can use the PHP string position function. In the below example we will search “you” in the string “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

String compare function: Strcmp()-

The strcmp() function stands for the string compare function. It is used to compare two string. If you want to compare two string words to each other then you can use PHP readymade function strcmp(). If the strings are the same, you will get 0 ( zero) as output. The following example will help you understand strcmp() function.

Example

Name :


Compare :

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

String to lower function : Strtolower():-

The strtolower() function means to convert the uppercase string to lowercase. The strtolower() function stands for the string to lower. If you want to convert a capital word (uppercase string ) to lowercase, then you can use string to lower function. It is also a readymade string function of PHP. >
Let's have a look at an example.

String - HELLO

Function - strtolower()

Output- hello

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

String to upper function: Strtoupper()-

The Strtoupper() is opposite to string to lower function. It converts lower case string to uppercase. As the name indicates uppercase, the string to upper function is followed same.

The following example will help you to convert lowercase string to uppercase.

Example

Enter Small String :

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

Please Share

Recommended Posts:-

Previous Posts:-

Featured Items:-


Ecommerce system in PHP website | Digital Ecommerce Shop web app

$66



Complete Blog CMS system in PHP with MYSQL database | PHP scripts

$13



Integrate Payumoney payment gateway in PHP website | Source Scripts

$12




0 Comment