JavaScript Tutorial

PHP String Functions

A string is a combination of characters and string functions in a programming language are used to manipulate strings. For example:

$string = “PHP string handling functions.”;

Difference between single and double quoted strings in Php

In single quoted strings, variables are not interpreted and the whole string is printed as it is. However, in double quoted strings PHP has to parse the variables, examine their values and print accordingly.

Code example

Escape sequencing in Php:

Escape sequences are used to escape a character while string parsing and giving an additional meaning to it like line breaks, tabs etc. Escape sequences work in double quote strings. Escape sequences start with a backslash() followed by a character. Escape sequences used in PHP are as follows:

Is replaced by a single quote

Is replaced by a double quote

 

To add line breaks within string

 

To add tab space

 

For carriage return

$

Is replaced by a dollar sign itself

Code example

Php string handling functions

Function Name Description

addcslashes

Returns a string after adding backslashes in front of mentioned character

addcslashes(string, characters).   //syntax

addslashes

Returns a string after adding backslashes in front of predefined characters (‘,”,,NULL)

addslashes(string)  //syntax

bin2hex

Converts binary to hexadecimal

bin2hex(string)   //syntax

chop

Removes whitespace or any other predefined character from the right end of a string

chop(string, character_list)  //syntax

chr

Converts a number to single-byte string

chr(ascii_code)  //syntax

chunk_split

Splits a string into smaller chunks

chunk_split(string, string_length, end)   //syntax

convert_cyr_string

Converts one cyrillic character set to another

convert_cyr_string(string, string_length, end)   //syntax

convert_uudecode

Decodes uuencoded string

convert_uudecode(string)   //syntax

convert_uuencode

Encodes the string in uuencode algorithm of encoding

convert_uuencode(string)   //syntax

count_chars

Information about characters used in the string

count_chars(string, mode)   //syntax

crc32

Calculates crc32 of a string

crc32(string)   //syntax

crypt

One way string hashing

crypt(string, salt)   //syntax

 

echo

Output data or result on screen

echo(string)   //syntax

explode

Splits a string into an array

explode(separator,string, limit)   //syntax

fprintf

Writes a formatted string to any stream

fprintf(stream,format,argument1, argument2, argument++) //syntax  

get_html_translation_table

Returns the translation table used by htmlspecialchars and htmlentities

get_html_translation_table(table, flag, encoding)   //syntax

hebrev

Converts hebrew text to visual text

hebrev(string, max_chars_per_line)   //syntax

 

hebrevc

Converts hebrew text to visual text and to <br>

hebrevc(string, max_chars_per_line)  //syntax

 

hex2bin

Converts hexadecimal to binary

hex2bin(string)   //syntax

html_entity_decode

Converts html entities to characters

html_entity_decode(string, flag, encoding)   //syntax

htmlentities

Converts characters to html entities

htmlentities(string, flag, encoding, double_encode)   //syntax

htmlspecialchars_decode

Converts special HTML entities to characters

htmlspecialchars_decode(string, flag)   //syntax

htmlspecialchars

Converts special characters to HTML entities

htmlspecialchars(string, flag, encoding, double_encode)   //syntax

implode

Binds an array into a string

implode(binder, array)   //syntax

join

Binds an array into a string

join(binder, array)   //syntax

lcfirst

Converts first character of a string to lowercase

levenshtein

Returns the levenshtein distance between two strings.Number of characters that will be replaced to transform string1 to string 2.

localeconv

Returns numeric formatting information

ltrim

Removes whitespace and other predefined characters from the left side of a string

md5

Calculates md5 hash of a string

md5_file

Calculates md5 hash of a file

metaphone

Calculates metaphone key of a string

money_format

Formats a number as currency

nl_langinfo

Locale information

nl2br

Inserts HTML line breaks in front of all new lines

number_format

Formats to number

ord

Returns the ASCII value of the first character of the string

parse_str

To parse a query string to a variable

print

Used to output string

printf

Prints a formatted string

quoted_printable_decode

Converts a quoted-printable string to an 8-bit string

quoted_printable_encode

Converts an 8-bit string to a quoted printable string

quotemeta

Quotes metacharacters

rtrim

Removes the whitespace or specified characters from the right side of the string

setlocale

Sets the locale information

sha1

Calculates the SHA-1 hash of a string

sha1_file

Calculates the SHA-1 hash of a file

similar_text

Checks for the similarity between two strings

soundex

Calculates the soundex key of a string

sprintf

Returns a formatted string

sscanf

Converts input from a string as per the given format

str_getcsv

Converts CSV string into an array

str_ireplace

Case insensitive while replacing some characters in a string.

str_pad

Pads a string to specified length

str_repeat

Repeats a string for a specified number of times

str_replace

Replaces specified characters in a string

str_rot13

Encodes a string by ROT13 method

str_shuffle

Shuffles a string randomly

str_split

Splits a string into an array

str_word_count

Counts the number of words in a string

strcasecmp

Case insensitive string comparison

strchr

Checks for the first occurrence of the string

strcmp

Case sensitive string comparison

strcoll

Locale based string comparison

strcspn

Returns the count of numbers found in a string before a matching mask

strip_tags

Strips HTML and PHP tags from a string

stripcslashes

Unquotes a string being quoted by addcslashes

stripslashes

Unquotes a quoted string

stripos

Case insensitive search of a string for the first occurrence of a string inside another

stristr

Case-insensitive search for the first occurrence of a string inside another string.

strlen

Returns the length of string

strnatcasecmp

Case insensitive comparison of two strings using Natural Order Algorithm

strnatcmp

Case sensitive comparison of two strings using Natural Order Algorithm

strncasecmp

Case insensitive comparison of strings for first n characters

strncmp

Case sensitive comparison of strings for first n characters

strpbrk

Search a string for any set of characters

strpos

Checks a string for the position of first occurrence of a specified string. Searching is case sensitive

strrchr

Checks a string for the last occurrence of a specified string

strrev

Reverse the string

strripos

Checks a string for the position of last occurrence of a specified string. Searching is case insensitive

strrpos

Checks a string for the position of last occurrence of a specified string. Searching is case sensitive

strspn

Returns the number of characters found in a string which has characters from specified charlist

strstr

Checks for the first occurrence of the string

strtok

Tokenize the string

strtolower

Converts whole string to lowercase

strtoupper

Converts whole string to UPPERCASE

strtr

Translate characters to string

substr

Returns a part of the string

substr_compare

Compare two strings with defined start position

substr_count

Count the number of substring existence in a string

substr_replace

Replaces substring with a defined string

trim

Removes whitespace and predefined characters from both sides of a string

ucfirst

Converts the first character of string to uppercase

ucwords

Converts the first character of each word in a string to uppercase

vfprintf

Writes a formatted string to a stream

vprintf

Prints a formatted string

vsprintf

Returns a formatted string

wordwrap

Wraps a string to a mentioned number of characters

Go back to Previous Course