PHP String :-

Techno PHP

A string like a variable that is created first.You can use the string stored in function and another way you also store in a variable .
In below example, we will create a string and will store in a variable after we will print the variable through echo.


Example


<?php
$str = "String goes here ";
echo "After string is goes here !";
echo $str ; 
?>
		
		
Run

Parameter:-
$str – A variable
String goes here – A string
Echo – Echo used for display the value.


PHP String functions:-

The Length of a String:-

The PHP strlen() function used to find out the length of string .
The following example of strlen() function .


Example

<?php
echo strlen("Learn PHP "); // outputs 9
?>
An other example with HTML Form (Input string in 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 PHP str_word_count() function used for counts the number of words of complete 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 PHP strrev() function 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 to replaces some characters with some other characters in a string.
The example below 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 for 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 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

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 complete string to upper case .


Example

Enter Small String :

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

Related