STRING FUNCTION IN PHP

Posted on Updated on

Image result for STRING FUNCTION IN PHPSTRING FUNCTION IN PHP

  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

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

?>

Output

20

  1. 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 validation of input fields.

Syntax

Str_word_count(string)

Example

<?php

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

?>

Output

3

  1. Reversing a String

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

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

?>

Output

syawduolCotemocleW

  1. Finding Text Within a String

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

echostrpos(“Welcome to Cloudways”,”Cloudways”);

?>

Output

11

  1. Replacing text within a string

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

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

?>

Output

Welcome to the programming world

  1. Converting lowercase into Title Case

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

Syntax

Ucwords(string)

Example

<?php

echoucwords(“welcome to the php world”);

?>

Output

Welcome To ThePhp World

  1. Converting a whole string into UPPERCASE

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

Syntax

Strtoupper(string);

Example

<?php

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

?>

Output

WELCOME TO CLOUDWAYS

  1. Converting whole String to lowercase

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

Syntax

Strtolower(string)

Example

<?php

echostrtolower(“WELCOME TO CLOUDWAYS”);

?>

Output

welcome to cloudways

  1. Repeating a String

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

Syntax

Str_repeat(string,repeat)

Example

<?php

echostr_repeat(“=”,13);

?>

Output

  1. 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

echostrcmp(“Cloudways”,”CLOUDWAYS”);

echo “<br>”;

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

echo “<br>”;

echostrcmp(“Cloudways”,”Hosting”);

echo “<br>”;

echostrcmp(“a”,”b”);//compares alphabetically

echo “<br>”;

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

?>

Output

1

0

-1

-1

-4

  1. 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

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

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

?>

Output

e to Cloudways

Welcome to

s

  1. Removing white spaces from a String

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”);

?>

Output

Wordpess Hosting

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.

Helpline: 9814666333, 8699444666

Email:info@technocampus.co.in

**************************************************************************************************************

Please fill the FORM and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s