Important PHP string functions

PHP string functions:
In this post we will look at some commonly used functions to manipulate strings.

Note: It is important for Interview questions.




strlen() function in PHP:
The PHP strlen() function returns the length of a string.
The example below returns the length of the string "Welcome to PHP":

Example

<?php

echo strlen("Welcome to PHP"); // OUTPUT: 14

?>

In above example "Welcome to PHP" this parameter passed to strlen() function and we used echo statement so it prints 14 as a output.
Welcome=7
to=2
PHP=3
2 sapces.

strtoupper() function in PHP:
The strtoupper() function converts a string to uppercase.
Example
Convert all characters to uppercase:

<?php

echo strtoupper("Hello WORLD!"); 
// OUTPUT: HELLO WORLD!

?>

strtolower() function in PHP:
The strtolower() function converts a string to lowercase.
Example
Convert all characters to lowercase:

<?php

echo strtolower("Hello WORLD."); // OUTPUT: hello world.

?>

ucfirst() function in PHP:
The ucfirst() function converts the first character of a string to uppercase.
Example
Convert the first character of "hello" to uppercase:

<?php

echo ucfirst("hello world!"); // OUTPUT: Hello world!

?>


ucwords() function in PHP:
The ucwords() function converts the first character of each word in a string to uppercase.
Example
Convert the first character of each word to uppercase:

<?php

echo ucwords("hello world"); // OUTPUT: Hello World!

?>

strrev() function in PHP:
The PHP strrev() function reverses a string:
Example:

<?php

echo strrev("Vanesh"); // OUTPUT: hsenaV

?>

strpos() function in PHP:
The PHP strpos() function searches for a specific text within a string.
If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.

The example below searches for the text "world" in the string "Hello world!":

Example
<?php

echo strpos("Hello world!", "world"); // OUTPUT: 6

?>

str_replace() function  in PHP:
 - Replace Text Within a String
The PHP str_replace() function replaces some characters with some other characters in a string.

The example below replaces the text "world" with "Dolly":

Example
<?php

echo str_replace("world", "Vanesh", "Hello world!"); 
// OUTPUT: Hello Vanesh!

?>

strstr() Function in PHP:

Example
Find the first occurrence of "world" inside "Hello world!" and return the rest of the string:

<?php

echo strstr("Hello world!","world");

?>

OUTPUT: world!
The strstr() function searches for the first occurrence of a string inside another string.
This function is case-sensitive. For a case-insensitive search, use stristr() function.
Important: It returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found.

str_repeat() function in PHP:
Repeats a string a specified number of times.

Example
Repeat the string "v" 10 times:

<?php

echo str_repeat("v",10); 
// OUTPUT: vvvvvvvvvv

?>

substr() function in PHP:
The substr() function returns a part of a string.

Example
Return "world" from the string:

<?php

echo substr("Hello world",6); 
// OUTPUT: world

?>

Above php string functions are very important, and important for Interview Questions.


No comments:

Post a Comment

We are here to listen you, Comment your valueable opinion...!!!