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.
It is possible to access a portion of a string through substr() function.
<?php $x = "You are my best friend"; $y = substr($x,9,2); echo $y; ?>
This will produce following result
my
Using PHP's built in function substr_replace(), it is possible to change a portion of a string.
<?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
Using PHP's built in function strlen(), it is possible to find the length of a string.
<?php $x = "GoodMorning"; y = strlen($x); echo y; ?>
This will produce following result
11
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.
<?php $x = "You are beautiful"; $y = strrev($x); echo $y; ?>
This will produce following result
lufituaeb era uoY
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.
<?php $x = "You are beautiful"; y = strpos($x,"beautiful"); echo y; ?>
This will produce following result
7
Using PHP's built in function str_word_count(), it is possible to count number of words present in a string.
<?php $x = "You are beautiful"; y = str_word_count($x); echo y; ?>
This will produce following result
3
Using PHP's built in function substr_count(), it is possible to count number of occurrences of a word in a string.
<?php $x = "You are very very beautiful"; y = substr_count($x,"very"); echo y; ?>
This will produce following result
2
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 .
<?php $x = "You are very very beautiful"; $y = strpbrk( $x, "abcd" ); echo $y; ?>
This will produce following result
are very very beautiful
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.
<?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; ?>
Using PHP's built in function strtoupper() and strtolower(), it is possible to convert string in upper and lower case.
<?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
Using PHP's built in function ucfirst() and ucwords(), it is possible to convert string in upper and lower case.
<?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
Using PHP's built in function addslashes() and ucwords(), it is possible to escap certain charater in a string.
<?php $string = "India's most famous city!"; echo addslashes($string); ?>
This will produce following result
India\'s most famous city!
Using PHP's built in function trim(), rtrim() and ltrim(), it is possible to remove whitespace from begining and end of a string.
<?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!
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.
<?php $url = "https://www.techstrikers.com"; $url_parts = parse_url($url); ?>
Using PHP's built in function urlencode() and urldecode(), it is possible to encode and decode url.
<?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