12 Basic PHP String Functions For Beginners.

php string function

12 Basic PHP String Functions For Beginners.PHP String Functions

 

PHP String FunctionsAfter getting a full understanding of PHP DATA TYPES AND VARIABLE CONCEPTS, we are going to cowl basic PHP string functions during this tutorial.

php string function

PHP provides variety of inherent string functions that facilitate in activity many operations whereas handling string information


Basic PHP String Functions

In this section, square measure going to discuss many basic string functions that are most ordinarily utilized in PHP scripts.

PHP String Functions

Read More: HOW TO QUICKLY LEARN ANDROID APP DEVELOPMENT ONLINE

1. Getting length of a String

PHP has a predefined function to get the length of a string. Strlen() displays the length of any string. It is more commonly used in validating input fields where the user is limited to enter a fixed length of characters.

Syntax

Strlen(string);

Example

<?php

echo strlen(“Welcome to Cloudways”);//will return the length of given string

?>

Output

20

2. Counting of the number of words in a String

Another function which enables display of the number of words in any specific string is str_word_count(). This function is also useful in a validation of input fields.PHP String Functions

Syntax

Str_word_count(string)

Example

<?php

echo str_word_count(“Welcome to Cloudways”);//will return the number of words in a string

?>

Output

3

Read More:  HOW TO BECOME A SUCCESSFUL BLOGGER : 18 STEPS TO FOLLOW

3. Reversing a StringPHP String Functions

Strrev() is used for reversing a string. You can use this function to get the reverse version of any string.

Syntax

Strev(string)



Example

<?php

echo strrev(“Welcome to Cloudways”);// will return the string starting from the end

?>

Output

syawduolC ot emocleW

4. Finding Text Within a StringPHP String Functions

Strpos() enables searching particular text within a string. It works simply by matching the specific text in a string. If found, then it returns the specific position. If not found at all, then it will return “False”. Strops() is most commonly used in validating input fields like email.

Syntax

Strpos(string,text);

Example

<?php

echo strpos(“Welcome to Cloudways”,”Cloudways”);

?>

Output

11

5. Replacing text within a stringPHP String Functions

Str_replace() is a built-in function, basically used for replacing specific text within a string.

Syntax

Str_replace(string to be replaced,text,string)

 

Example

<?php

echo str_replace(“cloudways”, “the programming world”, “Welcome to cloudways”);

?>

Output

Welcome to the programming world

6. Converting lowercase into Title CasePHP String Functions

Ucwords() is used to convert first alphabet of every word into uppercase.

Syntax

Ucwords(string)

 

Example

<?php

echo ucwords(“welcome to the php world”);

?>

Output

Welcome To The Php World

7. Converting a whole string into UPPERCASEPHP String Functions

Strtoupper() is used to convert a whole string into uppercase.

Syntax

Strtoupper(string);



Example

<?php

echo strtoupper(“welcome to cloudways”);// It will convert all letters of string into uppercase

?>

Output

WELCOME TO CLOUDWAYS

8. Converting whole String to lowercasePHP String Functions

Strtolower() is used to convert a string into lowercase.

Syntax
Strtolower(string)
Example

<?php

echo strtolower(“WELCOME TO CLOUDWAYS”);

?>

Output

welcome to cloudways

9. Repeating a StringPHP String Functions

PHP provides a built-in function for repeating a string a specific number of times.

Syntax

Str_repeat(string,repeat)

Example
<?php

echo str_repeat(“=”,13);

?>

Output

=============

10. Comparing Strings

You can compare two strings by using strcmp(). It returns output either greater than zero, less than zero or equal to zero. If string 1 is greater than string 2 then it returns greater than zero. If string 1 is less than string 2 then it returns less than zero. It returns zero, if the strings are equal.

Syntax

Strcmp(string1,string2)

Example

<?php

echo strcmp(“Cloudways”,”CLOUDWAYS”);

echo “<br>”;

echo strcmp(“cloudways”,”cloudways”);//Both the strings are equal

echo “<br>”;

echo strcmp(“Cloudways”,”Hosting”);

echo “<br>”;

echo strcmp(“a”,”b”);//compares alphabetically

echo “<br>”;

echo strcmp(“abb baa”,”abb baa caa”);//compares both strings and returns the result in terms of number of characters.

?>

OutputPHP String Functions
1

0

-1

-1

-4

PREMIUM BOOTSTRAP TEMPLATE/THEME IN FREE (LOW COST).

11. Displaying part of String

Through substr() function you can display or extract a string from a particular position.

Syntax

substr(string,start,length)



Example

<?php

echo substr(“Welcome to Cloudways”,6).”<br>”;

echo substr(“Welcome to Cloudways”,0,10).”<br>”;

?>

Output

e to Cloudways

Welcome to

s

12. Removing white spaces from a StringPHP String Functions

Trim() is dedicated to remove white spaces and predefined characters from a both the sides of a string.

Syntax

trim(string,charlist)

Example

<?php

$str = “Wordpess Hosting”;

echo $str . “<br>”;

echo trim(“$str”,”Wording”);

?>

OutputPHP String Functions

Wordpess Hosting

pess Host

Hence, in this tutorial we have learned the most commonly used PHP string functions. If you have any questions, leave them in the Comments section and I will happily respond to them. 🙂

Please follow and like us:
0

Related posts

Leave a Comment