Strings In PHP

In this post, we will learn about PHP strings and their functions, examining the methods available for working with them.

A PHP string represents text or data as a sequence of characters. If you’re building a dynamic website, processing form submissions, or working with databases, you’ll often find yourself working with strings.



PHP Strings Function

PHP strings are a powerful, and the built-in functions available in the language make it easy to manipulate and process them in a variety of ways.

From searching and replacing text to formatting and encoding, PHP provides a wealth of functions to handle string data.

In this section of article, we will explore some of the most commonly used PHP string functions and their applications in web development.


PHP Strings Function strlen()

In PHP, strlen() calculates the length of a string.

Taking a string “Good morning” as an argument, the function returns how many characters the string contains.

Calculate the length of the string “Good morning ”:

Example: 

<?php echo strlen("Good morning");?>

Example: 

<?php echo strlen("Elon Musk, the CEO of Tesla"); ?>

PHP Strings Function str_word_count()

The str_word_count function is a built-in PHP function that counts the number of words in a string.

Syntax:

int str_word_count ( string $string [, int $format = 0 [, string $charlist ]] )
  • $string: The input string.
  • $format (optional): Specifies how to handle the words in the string. The following values are valid:
    0 – returns the number of words found.
    1 – returns an array where the keys are the positions of the words in the string.
    2 – returns an associative array, where the keys are the words found in the string.
  • $charlist (optional): Specifies a list of additional characters which will be considered as ‘word’ characters.

Now Count the words in “Good morning ”:

Example: 

<?php echo strlen("Good morning"); ?>

Example: 

<?php echo strlen("Elon Musk, the CEO of Tesla"); ?>

PHP Strings Function strrev()

The strrev() function is a built-in PHP function that reverses a string.

Syntax:

string strrev ( string $string )

In below example, we are reversing a string “Good morning”:

Example: 

<?php echo strrev("Good morning"); // return gninrom dooG ?>

Example: 

<?php echo strrev("Elon Musk, the CEO of Tesla"); // return alseT fo OEC eht ,ksuM nolE ?>

PHP Strings Function strpos()

The strpos() function is a built-in PHP function that finds the position of the first occurrence of a substring within a string.

Once a match is found, the function returns the character position that matches the first – Else it will return FALSE if no match was found.

Syntax:

int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

Find the text “morning” in “Good morning”:

Example: 

<?php echo strpos("Good morning, it's a sunny morning", "morning"); // return 5 ?>

In above example we see the strpos() function returns the position of the first occurrence of the substring within the string so it returns 5, the first “morning” and does not count the second occurrence of “morning”.

Example: 

<?php echo strpos("Elon Musk, the CEO of Tesla and he is also the CEO of SpaceX", "CEO"); // return 15 ?>

As we see in another example the strpos() function returns the position of the first occurrence and it returns 15, the first “CEO” and does not count the second occurrence of “CEO”.

Hint: The first character of a string is 0 (not 1).

Remember: strpos() selects the first occurrence of a word in the sentence.


PHP Strings Function str_replace()

Str_replace() replaces some characters in a string with other characters.

Replace “morning” with “afternoon”:

Example: 

<?php echo str_replace("morning", "afternoon", "Good morning"); // outputs Good afternoon ?>

Example: 

<?php echo str_replace("Tesla", "SpaceX", "Elon Musk, the CEO of Tesla"); // outputs Elon Musk, the CEO of SpaceX ?>
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *