Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
PHP String Functions
Next article icon

PHP String Functions

Last Updated : 03 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Strings are a fundamental data type in PHP, used to store and manipulate text. PHP provides a wide variety of built-in string functions. These functions perform various operations such as string transformations, character manipulations, encoding and decoding, and formatting, making string handling simpler and more efficient in PHP.

In this article, we will discuss the most common PHP string functions..

PHP String Functions

The PHP string functions are the inbuilt in PHP. The complete list of PHP string functions is given below:

PHP String FunctionsDescription
addcslashes()Adds backslashes before specified characters in a given string.
addslashes()Returns a string with backslashes before predefined characters like single quotes, double quotes, backslashes, and NULL.
bin2hex()Converts binary data into a hexadecimal representation.
chop()Alias for rtrim(). Removes whitespace or other specified characters from the end of a string.
chr()Returns a character from the specified ASCII value.
chunk_split()Splits a string into smaller chunks of a specific length, adding a separator (such as a newline).
convert_uudecode()Decodes a uuencoded string.
convert_uuencode()Encodes a string using the uuencode algorithm, typically used for transferring binary data over networks.
count_chars()Returns information about the frequency of every byte (ASCII character) in a string.
crc32()Calculates a 32-bit CRC (cyclic redundancy checksum) for error-checking data integrity.
PHP crypt()Encrypts a string using one-way DES, Blowfish, or MD5 hashing.
password_hash()Creates a password hash using one of several hashing algorithms, returning a hashed string.
echoOutputs one or more strings directly to the browser.
explode()Splits a string into an array by a specified delimiter.
hex2bin()Decodes a hexadecimal string into its binary representation.
implode()Joins array elements into a string, using a specified separator.
join()Alias for implode(). Joins array elements into a string.
lcfirst()Converts the first character of a string to lowercase.
levenshtein()Calculates the Levenshtein distance between two strings, measuring the difference between them.
ltrim()Removes whitespace or specified characters from the beginning of a string.
md5_file()Generates the MD5 hash of a file.
md5()Computes the MD5 hash of a string.
metaphone()Returns the metaphone key (phonetic algorithm) of a string, used for phonetic comparisons.
nl2br()Inserts HTML <br> tags before all newlines in a string.
number_format()Formats a number with grouped thousands and customizable decimal places.
ord()Returns the ASCII value of the first character of a string.
parse_str()Parses a query string into variables.
quoted_printable_decode()Decodes a quoted-printable encoded string into an 8-bit string.
quoted_printable_encode()Converts an 8-bit string to a quoted-printable encoded string.
quotemeta()Adds a backslash in front of characters that have special meanings in regular expressions.
rtrim()Removes whitespace or specified characters from the right end of a string.
sha1_file()Calculates the SHA-1 hash of a file.
sha1()Computes the SHA-1 hash of a string.
similar_text()Calculates the similarity between two strings as a percentage.
soundex()Returns the Soundex key of a string for phonetic comparison.
str_pad()Pads a string to a certain length with another string.
str_repeat()Repeats a string a specified number of times.
str_replace()Replaces all occurrences of the search string with a replacement in the subject string.
str_rot13()Applies the ROT13 encoding, shifting every alphabetic character by 13 places.
str_shuffle()Randomly shuffles the characters of a string.
str_split()Splits a string into an array, with each element being a substring of a specified length.
str_word_count()Returns the number of words in a string, or optionally returns an array of the words.
strcasecmp()Case-insensitive comparison of two strings.
strchr()Finds the first occurrence of a string in another string (alias of strstr()).
strcmp()Compares two strings in a case-sensitive manner.
strcoll()Locale-based string comparison.
strcspn()Returns the length of a segment in a string that doesn't contain specified characters.
strip_tags()Strip HTML and PHP tags from a string.
stripos()Finds the position of the first occurrence of a substring in a string (case-insensitive).
stripslashes()Removes backslashes added by addslashes().
stristr()Case-insensitive version of strstr(), finds the first occurrence of a substring.
strlen()Returns the length of a string.
strnatcasecmp()Case-insensitive "natural order" string comparison, where numbers are compared numerically.
strnatcmp()"Natural order" string comparison, where numbers are compared numerically.
strncasecmp()Case-insensitive comparison of the first n characters of two strings.
strncmp()Compares the first n characters of two strings in a case-sensitive manner.
strpbrk()Searches a string for any of a set of specified characters.
strpos()Finds the position of the first occurrence of a substring in a string (case-sensitive).
strrchr()Finds the last occurrence of a character in a string.
strrev()Reverses a string.
strripos()Finds the position of the last occurrence of a string (case-insensitive).
strrpos()Finds the position of the last occurrence of a string (case-sensitive).
strspn()Returns the length of the initial segment of a string that contains only specified characters.
strstr()Finds the first occurrence of a substring in a string (alias of strchr()).
strtok()Tokenizes a string into smaller parts based on specified delimiters.
strtolower()Converts a string to lowercase.
strtoupper()Converts a string to uppercase.
strtr()Translates certain characters in a string with other characters.
substr_compare()Compares two strings from a specified start position up to a specified length.
substr_count()Counts the number of occurrences of a substring within a string.
substr_replace()Replaces a part of a string with another string, starting at a specified position.
substr()Returns a portion of a string starting at a specified index.
trim()Removes whitespace or other specified characters from both ends of a string.
ucfirst()Converts the first character of a string to uppercase.
ucwords()Converts the first character of each word in a string to uppercase.
vprintf()Outputs a formatted string using an array of values.
vsprintf()Returns a formatted string using an array of values.
wordwrap()Wraps a string to a specified width using a string break character.

Benefits of PHP String Functions (No Installation Required)

  • Core Library: PHP string functions are part of PHP’s core library, meaning no external libraries, packages, or extensions are needed.
  • Pre-installed Functions: These functions come pre-installed with PHP, allowing immediate use without setup or dependency management.
  • Simplifies Development: Developers can begin using string functions right away, without worrying about additional configuration steps.
  • Versatility: Functions are available for both simple tasks (e.g., changing letter cases, formatting numbers) and more complex operations (e.g., encoding/decoding).
  • Increased Efficiency: With built-in availability, developers can focus on solving problems and building applications instead of managing additional dependencies.
  • Reliable Tools: Developers can rely on fast, dependable tools for handling text data in any PHP application.

Next Article
PHP String Functions

C

chinmoy lenka
Improve
Article Tags :
  • Misc
  • Web Technologies
  • PHP
  • C-String-Question
  • Functions
  • PHP-array
  • PHP-function
Practice Tags :
  • Functions
  • Misc

Similar Reads

    PHP range() Function
    The range() function is an inbuilt function in PHP which is used to create an array of elements of any kind such as integer, alphabets within a given range(from low to high) i.e, list's first element is considered as low and last one is considered as high. Syntax: array range(low, high, step) Parame
    3 min read
    PHP | time() Function
    The time() function is a built-in function in PHP which returns the current time measured in the number of seconds since the Unix Epoch. The number of seconds can be converted to the current date using date() function in PHP. Syntax: int time() Parameter: This function does not accepts any parameter
    2 min read
    PHP | Functions
    A function in PHP is a self-contained block of code that performs a specific task. It can accept inputs (parameters), execute a set of statements, and optionally return a value. PHP functions allow code reusability by encapsulating a block of code to perform specific tasks.Functions can accept param
    8 min read
    PHP pos() Function
    The pos() is an inbuilt function in PHP which is used to return the value of the element in an array which the internal pointer is currently pointing to. The pos() function does not increment or decrement the internal pointer after returning the value. In PHP all arrays have an internal pointer. Thi
    2 min read
    PHP | sleep( ) Function
    The sleep() function in PHP is an inbuilt function which is used to delay the execution of the current script for a specified number of seconds. The sleep( ) function accepts seconds as a parameter and returns TRUE on success or FALSE on failure. If the call is interrupted by a signal, sleep() funct
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences