Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

PHP Strings

PHP Strings

PHP Strings

PHP String Functions

PHP provides a wide range of built-in string functions that make it easy to work with strings in your code. Here are some of the most commonly used string functions in PHP:

  1. strlen() : Returns the length of a string.
  2. strpos() : Searches for a substring within a string and returns the position of the first occurrence.
  3. str_replace() : Replaces all occurrences of a substring with another substring within a string.
  4. str_replace()  : Converts a string to lowercase.
  5. strtoupper() : Converts a string to uppercase.
  6. substr() : Returns a portion of a string.
  7. trim() : Removes whitespace (or other characters) from the beginning and end of a string.
  8. implode() : Joins an array of strings into a single string, separated by a delimiter.
  9. explode() : Splits a string into an array of substrings, using a delimiter.
  10. strrev() : Reverses a string.
  11. str_word_count() : Counts the number of words in a string.
  12. htmlentities() : Converts special characters to HTML entities.
  13. htmlspecialchars() : Converts special characters to their HTML entity equivalents.
  14. preg_replace() : Replaces text that matches a regular expression pattern with a specified string.
  15. substr_replace() : Replaces a portion of a string with another string.

Among the myriad functionalities present in PHP, the ones highlighted here represent merely a fraction of the extensive array of string functions at your disposal. By using these functions and others like them, you can manipulate strings in your code to accomplish a wide range of tasks, from simple formatting and cleanup to more complex parsing and searching.

 

str_word_count() – Count Words in a String

The str_word_count() function in PHP is used to count the number of words in a string. The syntax of the str_word_count() function is as follows:

str_word_count($string, $format, $charlist);

where $string is the string to be analyzed, $format specifies the output format, and $charlist is an optional list of additional characters to be considered as word characters.

Here’s an example of how to use the str_word_count() function:

$string = "The quick brown fox jumps over the lazy dog";
$word_count = str_word_count($string);
echo $word_count;

In this case, the variable $word_count will contain the value 9, because there are 9 words in the original string.

By default, str_word_count() returns the number of words in the string as an integer. However, you can also use the $format parameter to specify a different output format. The possible values for $format are:

  • 0 : returns the number of words (default)
  • 1 : returns an array of the words in the string
  • 2 : returns an associative array with the keys being the numeric position of each word in the string and the values being the words themselves

Here’s an example of how to use the $format parameter:

$string = "The quick brown fox jumps over the lazy dog";
$word_array = str_word_count($string, 1);
print_r($word_array);

In this case, the variable $word_array will contain the array [“The”, “quick”, “brown”, “fox”, “jumps”, “over”, “the”, “lazy”, “dog”] , because we used the $format parameter to return an array of the words in the string.

You can also use the $charlist parameter to specify additional characters that should be considered as word characters. For example, if you wanted to include hyphens as word characters, you could use the following code:

$string = "The quick-brown fox jumps over the lazy dog";
$word_count = str_word_count($string, 0, "-");
echo $word_count;

In this case, the variable $word_count will contain the value 9, because we included hyphens as word characters and counted “quick-brown” as a single word.

Overall, the str_word_count() function is a useful tool for counting the number of words in a string in PHP.

 

strrev() – Reverse a String

The strrev() function in PHP is used to reverse a string. The syntax of the strrev() function is as follows:

strrev($string);

where $string is the string to be reversed.

Here’s an example of how to use the strrev() function:

$string = "Hello, world!";
$reversed = strrev($string);
echo $reversed;

In this case, the variable $reversed will contain the value “!dlrow ,olleH”, because we reversed the original string.

The strrev() function works by taking each character in the string and appending it to the beginning of a new string, so the final result is a reversed version of the original string.

It’s important to note that strrev() only works with single-byte characters, so it may not work correctly with multi-byte strings (such as those containing non-ASCII characters). In those cases, you may need to use a different function or library to reverse the string.

Overall, the strrev() function is a useful tool for reversing strings in PHP, as long as you’re working with single-byte characters.

 

strpos() – Search For a Text Within a String

In PHP, strpos() is a function used to search for the position of a substring within a string. The function returns the numeric position of the first occurrence of the substring if it is found in the string. If the substring is not found, it returns false.

The syntax of the strpos() function is:

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

where $haystack is the string in which you want to search for the $needle substring, and $offset is the optional starting position for the search within the string.

In illustrating the utilization of the strpos() function within PHP, consider the following instance as a practical demonstration.

<?php
  $string = "Hello, world!";
  $substring = "world";
  
  if (strpos($string, $substring) !== false) {
    echo "The string '$substring' was found in the string '$string' at position " . strpos($string, $substring);
  } else {
    echo "The string '$substring' was not found in the string '$string'";
  }
?>

In this example, we are searching for the substring “world” within the string “Hello, world!”. If the substring is found in the string, the strpos() function will return the position of the first occurrence of the substring, which will be printed to the screen. If the substring is not found in the string, the function will return false, and the message “The string ‘world’ was not found in the string ‘Hello, world!'” will be printed to the screen.

 

str_replace() – Replace Text Within a String

str_replace() is a PHP function that is used to replace all occurrences of a string within another string. It takes three parameters: the string to search for, the string to replace it with, and the string to search within. The function returns a new string with all instances of the search string replaced with the replacement string.

Here’s an example usage of str_replace() :

$string = "Hello, world!";
$new_string = str_replace("world", "John", $string);
echo $new_string; // Output: Hello, John!

In this example, the str_replace() function searches for the word “world” in the $string variable, and replaces it with “John”. The resulting string, “Hello, John!”, is then stored in the $new_string variable and printed to the screen using the echo statement.

Scroll to Top