0% found this document useful (0 votes)
13 views2 pages

PHP String Functions Overview

The document outlines various PHP string functions categorized into six sections: string length and case functions, searching in strings, modifying strings, comparing strings, splitting and joining strings, and special string functions. Each section includes a function name, its description, and an example of usage. This serves as a reference for developers to manipulate and handle strings effectively in PHP.

Uploaded by

Raghav Chitkara
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

PHP String Functions Overview

The document outlines various PHP string functions categorized into six sections: string length and case functions, searching in strings, modifying strings, comparing strings, splitting and joining strings, and special string functions. Each section includes a function name, its description, and an example of usage. This serves as a reference for developers to manipulate and handle strings effectively in PHP.

Uploaded by

Raghav Chitkara
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PHP String Functions

1. String Length and Case Functions


Function Description Example

strlen() Returns the length of a string strlen("Hello") → 5

str_word_count() Counts number of words in a string str_word_count("Hello World") → 2

strtoupper() Converts string to uppercase strtoupper("hello") → HELLO

strtolower() Converts string to lowercase strtolower("HELLO") → hello

ucfirst() Capitalizes first letter ucfirst("hello") → Hello

lcfirst() Converts first letter to lowercase lcfirst("Hello") → hello

ucwords() Capitalizes first letter of each word ucwords("hello world") → Hello World

2. Searching in Strings
Function Description Example

strpos() Finds first position of substring strpos("Hello World", "World") → 6

strrpos() Finds last position of substring strrpos("Hello World World", "World") → 12

strstr() Finds first occurrence of string strstr("Hello World", "World") → World

strrchr() Finds last occurrence of string strrchr("Hello World", "o") → orld

substr() Returns part of a string substr("Hello World", 0, 5) → Hello

3. Modifying Strings
Function Description Example

str_replace() Replaces occurrences of a string str_replace("world", "PHP", "Hello world") → Hello PHP

substr_replace() Replaces part of a string substr_replace("Hello World", "PHP", 6) → Hello PHP

trim() Removes whitespace from both sides trim(" Hello ") → Hello

ltrim() Removes whitespace from left ltrim(" Hello ") → "Hello "

rtrim() Removes whitespace from right rtrim(" Hello ") → " Hello"

strrev() Reverses a string strrev("Hello") → olleH

addslashes() Adds backslashes before quotes addslashes("O'Reilly") → O\'Reilly

stripslashes() Removes backslashes stripslashes("O\'Reilly") → O'Reilly

4. Comparing Strings
Function Description Example

strcmp() Binary safe comparison (case-sensitive) strcmp("Hello", "hello") → -1


strcasecmp() Case-insensitive comparison strcasecmp("Hello", "hello") → 0

strncmp() Compares first n characters strncmp("Hello", "Helium", 3) → 0

strnatcmp() Natural order comparison strnatcmp("2", "10") → -1

similar_text() Calculates similarity percentage similar_text("Hello", "Helli") → 4

5. Splitting and Joining Strings


Function Description Example

explode() Splits string into array explode(" ", "Hello World") → [Hello, World]

implode()/join() Joins array into string implode(" ", ["Hello", "World"]) → Hello World

str_split() Splits string into characters str_split("Hello") → [H, e, l, l, o]

chunk_split() Splits into chunks chunk_split("HelloWorld", 2, "-") → He-ll-oW-or-ld-

6. Special String Functions


Function Description Example

number_format() Formats number with commas number_format(1000000) → 1,000,000

md5() Returns MD5 hash md5("hello")

sha1() Returns SHA1 hash sha1("hello")

htmlspecialchars() Converts chars to HTML entities htmlspecialchars("<a>") → &lt;a&gt;

nl2br("Hello
nl2br() Inserts <br> before newlines World") → Hello<br>World

You might also like