In this part, we are going to learn about PHP string functions. PHP has many built-in function that works with strings. PHP.net website has a list of these built-in function. You can check the list on the official website of PHP. 

PHP for beginners Part 7, Variables and String in php

Here are some of the commonly used string functions in PHP.

Strtoupper

 This function is used to turn all the character within a sentence to upper case.

 Here is a simple program that will turn all the character of sentence into upper case.

<?php

$para = “A quick brown fox jumps over a lazy dog”;

 echo strtoupper($para);

?>

 

The function will change all the character of that particular sentence to upper case with the help of strtoupper function. Here is the screenshot of output.

php string functions

 

 Strtolower function

This function is used to turn all the character within a sentence into lowercase.

Example

<?php

$para = “A quick brown fox jumps over a lazy dog”;

 echo strtolower($para);

?>

 

The function will change all the character of that particular sentence to lower case with the help of strtolower function. Here is the screenshot of output.

php string functions

 

ucfirst

This is another PHP in built function that use to turn the first character of a sentence into the uppercase letter.

Here is a simple program that will let you understand it more accurately.

<?php

$para = “a quick brown fox jumps over a lazy dog”;

 echo ucfirst($para);

?>

 

The function will change all the first character i.e. a of that particular sentence to uppercase A with the help of ucfirst function. Here is the screenshot of output.

php string functions

ucwords

Ucword is also a PHP built in function that use to turn the first character of each word in a sentence into the uppercase letter.

Here is a simple program that will let you understand it more accurately.

<?php

$para = “a quick brown fox jumps over a lazy dog”;

 echo ucword($para);

?>

 

The program will change the first character of each word in a particular sentence to upper case. It’s all due to the use of the ucfirst function. Here is the screenshot of output. Its look awesome. 😉

php string functions

   strlen

Here is another interesting built in PHP string function. Strlen will count the number of character within a sentence, Find something interestingJ isn’t it. 😉

Let’s write a simple program that will count the number of character within a paragraph.

  <?php

$para = “the quick brown fox jumped over the lazy dog”;

 echo strlen($para);

?>

Here is the screenshot of output. The total numbers of characters in this sentence are 45.

php string functions

str_replace

   This function can replace the particular word within a paragraph with another word of your choice. The function takes three arguments. The first argument is the word that you want to replace. The second argument is the new word that you want to add. And the third argument is the sentence or variable that are storing the sentence.

Here is the simple program that will let you understand that function more clearly.

<?php

 $para = “the quick brown fox jumped over the lazy dog.”;

echo str_replace(“quick”,”fast”, $para);

?>

 

The word quick will be replaced by fast in the above paragraph. Here is the screenshot of output.

php string functions

There are many more string functions in PHP. You can find other from this  link.

LEAVE A REPLY

Please enter your comment!
Please enter your name here