PHP String Manipulations

In computer programming, a string is an array or a sequence of characters. String is a basis data-type in PHP. PHP allows you to perform different operations on a string called PHP string manipulation for example, reversing words of a string, finding a portion of a string, encrypting a strings etc.

Finding Parts of String

It is possible to access a portion of a string through substr() function.

Finding Parts of String Example

This will produce following result

Replace Portion of a string

Using PHP's built in function substr_replace(), it is possible to change a portion of a string.

Replace Portion of a string Example

This will produce following result

Find Length of a String

Using PHP's built in function strlen(), it is possible to find the length of a string.

Find Length of a String Example

This will produce following result

Reverse a String

Using PHP's built in function strrev(), it is possible to reverse a string. It will not change the case of the letters of the string.

Reverse a String Example

This will produce following result

Search Position of a Word in a String

Using PHP's built in function strpos(), it is possible to search for the position of a given word in a string. If it finds the word in the string then the function returns the character position of its first match but If it does not find the word, function returns FALSE.

Search Position of a Word in a String Example

This will produce following result

Word Count in a String

Using PHP's built in function str_word_count(), it is possible to count number of words present in a string.

Word Count in a String Example

This will produce following result

Count Number of Occurrences of a Word in a String

Using PHP's built in function substr_count(), it is possible to count number of occurrences of a word in a string.

Count Number of Occurrences of a Word in a String Example

This will produce following result

Search for a set of Characters

Using PHP's built in function strpbrk(), it is possible to search for a set of characters in a string. The function returns the portion of the string from the first matched character to the end of the string. If no character in the set is found in the string, strpbrk() returns false .

Search for a set of Characters Example

This will produce following result

Encryption and Decryption of a String

Using PHP's built in function mcrypt_ecb(), it is possible to encrypt and decrypt of a string. The function uses 'TripleDEC' encryption.

Note: These built in encryption function work with the mcrypt library.

Encryption and Decryption of a String Example

Converting Case of a String

Using PHP's built in function strtoupper() and strtolower(), it is possible to convert string in upper and lower case.

Converting Case of a String Example

This will produce following result

Converting Case for First Word and All Words of a String

Using PHP's built in function ucfirst() and ucwords(), it is possible to convert string in upper and lower case.

Converting Case for First Word and All Words of a String Example

This will produce following result

Escaping Charater in a String

Using PHP's built in function addslashes() and ucwords(), it is possible to escap certain charater in a string.

Converting Case for First Word and All Words of a String Example

This will produce following result

Triming Blanks from a String

Using PHP's built in function trim(), rtrim() and ltrim(), it is possible to remove whitespace from begining and end of a string.

Triming Blanks from a String Example

This will produce following result

Parsing a URL

Using PHP's built in function parse_url() and parse_str(), it is possible to take raw URL. It is used to get the list of URL's constituent parts such as schema, domain, path and so on and return an associative array.

Parsing a URL Example

URL Encode

Using PHP's built in function urlencode() and urldecode(), it is possible to encode and decode url.

URL Encode Example

This will produce following result