Basic string functions in php

There are 12 basic functions in php they are as follows:

1.strlen() 

This function is used in php to return the length of a string.It most commonly used in validating a string in input field to check whether the user is is given minimum number of characters in the input field.

Syntax:

strlen(string);

2.str_word_count()

This function is used to count the number of words in a sentence. This function is also used in the input field open HTML page to validate the given input by the user. For example if we given a sentence 'This is a simple website', then it will return a value 5.

Syntax:

str_word_count(string);

3.strrev()

Which function used in PHP is used to reverse a string. For example if we give the word 'CSPSYCO' then which will return a string 'OCYSPSC'.

Syntax:

strrev(string);

4.strpos()

This function is used to find a particular string from a group of sentences, returning the position of the string in a sentence. For example if you are given a sentence 'this is a website' and we search for the string website, return to value 10.If the string is not found,it returns false. The counting starts from 0.

Syntax:

strpos(string);

5.str_replace()

The str_repace() function is used in PHP to replace a string in a sentence with a given sentence.

eg:
<?php echo str_replace(”CSPSYCO”,"the programming world","Welcome to CSPSYCO”);

Output

Welcome to the Programming world

6.ucwords()

This function converts first alphabet of every word to uppercase letter.

Syntax:

ucwords(string);

eg:
ucwords("this website is hosted by blogspot");

Output

This Website Is Hosted By Blogspot


7.strtoupper()

This function convert every alphabet in a string to uppercase letter.

Syntax:

strtoupper("this website is hosted by blogspot");

Output

THIS WEBSITE IS HOSTED BY BLOGSPOT

8.strtolower()

This is just opposite to the strtoupper() function. That is it converts every alphabet in a string to lower case letters.

Syntax:

strtolower(string);

9.str_repeat()

This function is used to display repeatedly a string to a given number of times.

Syntax:

str_repeat(string,number of times);

Eg:

str_repeat("+",8);

Output

++++++++

10.strcmp()

This function is used to compare two strings.It returns three type of value,a value less than 0, 0,a value greater than 0. If the first string is greater than the second string,then it will return greater than 0 value. If the first string is less than the second string,then it will return less than 0 value. If the first string is equal to the second string then it will return 0.

Syntax:

strcmp(string1, string2);

11.substr

Through this function,you can extract or display a string/substring from a given position.

Syntax:

substr(string,start,length);

12.trim()

It is used to remove or to trim pre defined characters from a both sides of a string.

Syntax:

trim(string,charlist);

Eg:

trim("blogging website","blogsite");

Output

ing web


Prev
Next

Comments

Post a Comment

Popular posts from this blog

Node.js Cheat Sheet

Enhancing Supply Chain Security and Cloud-Native Development with Chainguard

Bootstrap ? What is it ?