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

<?php 
$x = "You are my best friend";
$y = substr($x,9,2);
echo $y;
?> 

This will produce following result

my

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

<?php 
$x = "You are my best friend";
$y = substr_replace("my","our",$x);
echo $y;
?> 

This will produce following result

You are our best friend

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

<?php 
$x = "GoodMorning";
y = strlen($x);
echo y;
?> 

This will produce following result

11

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

<?php 
$x = "You are beautiful";
$y = strrev($x);
echo $y;
?> 

This will produce following result

lufituaeb era uoY

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

<?php 
$x = "You are beautiful";
y = strpos($x,"beautiful");
echo y;
?> 

This will produce following result

7

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

<?php 
$x = "You are beautiful";
y = str_word_count($x);
echo y;
?> 

This will produce following result

3

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

<?php 
$x = "You are very very beautiful";
y = substr_count($x,"very");
echo y;
?> 

This will produce following result

2

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

<?php 
$x = "You are very very beautiful";
$y = strpbrk( $x, "abcd" );
echo $y;
?> 

This will produce following result

are very very beautiful

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

<?php 
$key = "techstrikers";
$string = "Learn Techcnologies in Simple and Fast Way";
$encrypted_string = mcrypt_ecb(MCRYPT_TripleDEC,$key,$string, MCRYPT_ENCRYPT);
echo $encrypted_string;
$decrypted_string = mcrypt_ecb(MCRYPT_TripleDEC,$key,$encrypted_string, MCRYPT_DECRYPT);
echo $decrypted_string;
?> 

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

<?php 
$string = "You are very very beautiful";
echo strtoupper($string);
echo strtolower($string);
?> 

This will produce following result

YOU ARE VERY VERY BEAUTIFUL
you are very very beautiful

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

<?php 
$string = "you are very very beautiful";
echo ucfirst($string);
echo ucword($string);
?> 

This will produce following result

You are very very beautiful
You Are Very Very Beautiful

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

<?php 
$string = "India's most famous city!";
echo addslashes($string);
?> 

This will produce following result

India\'s most famous city!

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

<?php 
$string1 = "India's most famous city!   ";
echo rtrim($string1); // remove whitespace from right side of a string
$string2 = "  India's most famous city!";
echo ltrim($string2); // remove whitespace from left side of a string
$string3 = " India's most famous city!     ";
echo trim($string3); // remove whitespace from both side of a string
?> 

This will produce following result

India's most famous city!
India's most famous city!
India's most famous city!

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

<?php 
$url = "https://www.techstrikers.com";
$url_parts  = parse_url($url); 
?> 

URL Encode

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

URL Encode Example

<?php 
$url = "https://www.techstrikers.com/some string/Johan's Pet";
echo urlencode($url); 
echo urldecode($url); 
?> 

This will produce following result

https://www.techstrikers.com/some+string/Johan%27s+Pet
https://www.techstrikers.com/some string/Johan's Pet