Home Blog Strings in PhP

Strings in PhP

0
Strings in PhP

A string is a sequence of characters, like “Hello world!”.. PHP has over 75 built-in String manipulation functions, supporting operations ranging from string repetition and reversal to comparison and search-and-replace.

Some of these important functions are

  1. empty() – Tests if a string is empty
  2. strlen() – Calculates the number of characters in a string
  3. strrev() – Retrun reverse of a given string
  4. str_repeat() – Repeats a string no. of times you want
  5. substr() – Retrieves a section of a string
  6. strcmp() – Compares two strings
  7. str_word_count() – Calculates the number of words in a string
  8. str_replace() – Replaces parts of a string
  9. trim() – removes leading and trailing whitespaces from a string
  10. strtolower() – Converts in Lowercases a string
  11. strtoupper() – Converts in Uppercases a string
  12. ucfirst() – Converts in uppercase the first character of a string
  13. ucwords() – Converts in uppercases the first character of every word of a string
  14. addslashes() – Escapes special characters in a string with backslashes
  15. stripslashes() – Removes backslashes from a string
  16. htmlentities() – Encodes HTML within a string
  17. htmlspecialchars() – Encodes special HTML characters within a sting
  18. nl2br() – Replaces line breaks in a string with elements
  19. html_entity_decode() – Decodes HTML entities within a string
  20. htmlspecialchars_decode() – Decodes special HTML characters withing a string
  21. strip_tags() – Removes PHP and HTML code from a string
  22. md5() – The MD5 message-digest algorithm is a widely used for cryptography
  23. wordwrap – Wraps a string to a given number of characters
  24. print – Output a string
  25. printf – Output a formatted string

Click here to find the complete list of String Functions in PhP

Example to get the Length of the String

The PHP strlen() function returns the length of a string.

<?php
echo strlen("Hello world!");
?>

Output: 12

Example to Replace Text Within a String

The PHP str_replace() function replaces some characters with some other characters in a string. Example is to replaces the text “world” with “Dolly”

<?php
echo str_replace("world", "Dolly", "Hello world!");
?>

Output: Hello Dolly!

Example to Count The Number of Words in a String

The PHP str_word_count() function counts the number of words in a string

<?php
echo str_word_count("Hello world!");
?

Output: 2

Previous article Data Types in PhP
Next article Operators in PhP
Hi!, I am Pardeep Patel, an Indian passport holder, Traveler, Blogger, Story Writer. I completed my M-Tech (Computer Science) in 2016. I love to travel, eat different foods from various cuisines, experience different cultures, make new friends and meet other.

LEAVE A REPLY

Please enter your comment!
Please enter your name here