Top 10 Most Popular and Commonly Used PHP String Functions

The Top 10 Most Popular PHP String Functions10 Useful PHP Functions and Features You Need to Know

Top 10 Most Popular and Commonly Used PHP String Functions

The Top 10 Most Popular PHP String Functions

10 Useful PHP Functions and Features You Need to Know

  1. substr() to find a sub string of a string

The substr() PHP function is very frequently used in PHP files. It is used to get a sub string or part of a string. It gets the start point and the length of characters and the string. The return type of this function is a string (See Data Types in PHP). See the Prototype below:

string substr(string string, int start[, int length] );

PHP Function Example:

$name = “How to Code”;

substr($name, 0, 3);

  1. strlen() to find out length of a string

The strlen() PHP function is used to get the length of a given string. The return type of this function is integer.

PHP Function Example:

$name = “How to Code”;

strlen($name);

  1. str_replace() is used to replace a string with another string in a given string

The str_replace() PHP function is used to replaces or change some characters with some other characters in a given string.

See the proto type below:

mixed str_replace(mixed needle, mixed new_needle, mixed haystack[, int &count]));

Here we are replacing the characters “world” in the string “Hello world!” with “HowToCode”:

echo str_replace(“world”,” HowToCode”,”Hello world!”);

  1. trim() function is used to remove white spaces or and give characters from a string

The trim() PHP function is used to remove whitespace or any other predefined or given characters from both sides of a string.

trim(string,charlist)

Similar function:

ltrim() – It is used to remove whitespace or other predefined or given characters from the left side of a given string

rtrim() – It is used to remove whitespace or other predefined or given characters from the right side of a given string

Example:

$name = “HowtoCode”;

trim($name,”He!”);

Result: owtoCod

  1. strpos() is used to find the position of given characters in a string

The strops() PHP function is used to get the position of a needle within a haystack. It returns the first occurrence of a string inside another given string.

strpos() function prototype:

int strpos(string haystack, string needle, int [offset] );

strpos(string,find,start);

Example:

echo strpos(“Learn How to code, How to make a website”,”How”);

Other similar PHP functions:

strrpos() – Used to find the position of the last occurrence of a string in a given string (case-sensitive)

stripos() – Used to find the position of the first occurrence of a string in a given string (case-insensitive)

strripos() – Used to find the position of the last occurrence of a string in a given string (case-insensitive)

  1. strtolower() is used to convert all characters to lowercase characters

The strtolower() PHP function is used to convert all the characters of a given string to all lower case characters. The function is very useful in comparison of strings.

Example:

strtolower(string)

$name = “HowToCode”;

echo strtolower($name);

Result: howtocode

  1. strtoupper() is used to convert all characters to uppercase characters

The strtolower() PHP function is used to convert all the characters of a given string to all upper case characters.

Example:

strtoupper (string)

$name = “HowToCode”;

echo strtoupper ($name);

Result: HOWTOCODE

  1. is_string() is to used to check that the type of a given variable is string

The is_string() PHP function is used to check that the type of a given variable is string or not.The is_string() PHP function returns a Boolean value of true or false.

Example:

is_string(‘howtocode’) = bool(true)

is_string(123) = bool(false)

  1. strstr() used to find a string or character match within a longer string

The strstr()function in PHP searches for the first occurrence or position of a string inside another given string. The function strstr()in php can be used to find a string or character match within a longer string. The return type of this function is string.

See the proto type:

string strstr(string haystack, string needle);

strstr(string,search,before_search)

  1. ucwords() Function in PHP is used to convert the first character of each word to uppercase

The PHP ucwords() Function is used to turn all the first characters of all the words to the upper case letters in a given string.

Example:

$str = ‘learn coding’;

echo ucwords($str);

Result: Learn Coding

Learn PHP with HowtoCode.PK .

See More:

Brief History of PHP Programming Language

Comments