Day -7 Bhavik PHP String Function with example bhavikphp


STRING REPLACE
(I)
$bhavik = str_replace("college", "school", "I love my college");
Print $bhavik;
This is output: I love my school
(II)
$numbers = array("1", "2", "3");
$words = array("one", "two", "three");
$phrase = "I have only 1 girlfriend and 3 friends";
$bhavik = str_replace($numbers, $words, $phrase);
Print $bhavik;

This is output: I have only one girlfriend and three friends
...........................................................

STRING POSITION

echo strpos("Hello BHAVIK!","BHAVIK");

output:- 6
.........................................................................................

STRING LENGTH

echo strlen("Hello BHAVIK");

output:-12
............................................................

String To Lowercase

The strtolower() function converts a string to lowercase.

echo strtolower("Hello BHAVIK.");
output:-
hello bhavik.
............................................................

SUB STRING

The substr() function returns a part of a string.
substr(string,start,length)

echo substr("Hello BHAVIK!",6);

The output :BHAVIK
!
..............................................................

STRING WORD COUNT

If you want to find out the number of words in a string, you can use str_word_count():

$bhavik = 'bhavik, dutt!';
echo str_word_count( $bhavik );

Output:- "2"
................................................................

0 comments:

Post a Comment

Newer Post Older Post Home