PHP String Functions

PHP provides a rich set of built-in string functions that allow developers to manipulate and process text efficiently.

These functions enable operations such as searching, replacing, formatting, splitting, and comparing strings, among other tasks.

Below is a categorized list of PHP string functions based on their functionality.

String Manipulation and Formatting

FunctionsDescription
addslashes()Escape Special Characters
chop()Remove blank space at the end of the string
chunk_split()Break string after a position
str_split()Split a string with fixed intervals (without using any delimiter)
str_pad()Padding string from left, right, or both sides
strrev()Reversing a string
stripslashes()to Remove Backslashes
sprintf()Formatting strings for numbers, and other data types
nl2br()Converting line breaks to HTML <BR> tags
str_shuffle()Shuffle a string
str_repeat()Repeat a string multiple times
trim()Removing char or blank space from left, right, and both sides of a string
wordwrap()Wrapping the string after a width

String Search and Substring Extraction

FunctionsDescription
strpos()Case-sensitive search returns position of the first occurrence
stripos()Case-insensitive search returns position of the first occurrence
strrpos()Case-sensitive search returns position of the last occurrence
strripos()Case-insensitive search returns position of the last occurrence
strchr()Return the substring from the matching point
strrchr()Find the last occurrence of a character and return the substring
strstr()Search for string with needle (case-sensitive)
stristr()Search for string with needle (case-insensitive)
substr()Extract part of the string based on position and length
substr_count()Count the number of times a substring occurs in a string

String Replacement

FunctionsDescription
str_replace()Case-sensitive string replacement
str_ireplace()Case-insensitive string replacement
preg_replace()Regular expression string replacement

String Case Conversion

FunctionsDescription
strtolower()Convert all characters to lowercase
strtoupper()Convert all characters to uppercase
lcfirst()Convert the first character to lowercase
ucfirst()Convert the first character to uppercase
ucwords()Convert the first character of every word to uppercase

String Comparison

FunctionsDescription
strcasecmp()Case-insensitive string comparison
strcmp()Case-sensitive string comparison
strcoll()locale-based string comparison
similar_text()Comparing strings to get the percentage of similarity

String Operations with Arrays and Delimiters

FunctionsDescription
explode()Break a string into an array using a delimiter
implode()Join array elements into a string
split()Break a string using a delimiter

ASCII and Character Functions

FunctionsDescription
chr()Return a character from an ASCII value
ord()Return the ASCII value of a character
count_chars()Return information about the characters used in a string

Encryption and Hashing

FunctionsDescription
md5()String encryption using MD5 hash
password_hash()One-way password hash
password_verify()Verify a string against a password hash
random_bytes()Cryptographically secure random bytes (PHP 7+)
mcrypt()provide encryption functions ( DEPRECATED )

HTML and Special Characters

FunctionsDescription
htmlspecialchars()Convert special HTML characters to their entities
strip_tags()Remove HTML tags from a string

CSV Handling

FunctionsDescription
fgetcsv()Get data from a CSV file
fputcsv()Write data to a CSV file

Miscellaneous Functions

FunctionsDescription
addcslashes()Add slashes to specific characters
str_word_count()Count the words in a string
strlen()Return the length of a string

Scripts using String functions in PHP

Palindrome
Check the input string is Palindrome or not

ctype_lower() & ctype_upper()
checking presence of lower or upper case letter only.

Checking String
Checking string for presence of Upper case or lower case letter or special characters or space.

Random string
We can generate random string by using rand function

Removing chars from end
Remove chars from end of the string by using length and sub string functions

Adding strings
Add two or more strings including date.

Collecting part of string
Extracting string part using two landmark points

Email part
Getting the domain and Userid part from an email address

Questions


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    satish

    13-03-2013

    Its good information




    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer