Learn Working with Strings in PHP
Manipulation of strings is an important concept in PHP. Various operations can be performed on strings including finding the string length, search for position of specific characters in the string, replace existing characters with new values and much more.
PHP is an open source scripting language widely popular as an effective web development platform on which several websites today are developed. Working with strings is one of the most important concepts taught in PHP training classes. Strings can be defined as a sequence of characters which may include anything like letters, symbols and numbers. For example: Hello, Hello! , Hello!@123World*^# is all strings.
There are various operations that can be performed on strings using PHP and to perform them one needs to learn about some valuable functions used in PHP programming.
The simple of all functions used for strings is strlen() function which returns an integer value showing the number of characters present in the string.
For example: The statement strlen(“Hello!”) will return value 6 as the output. The strlen function is particularly useful when one wants to find the end of string for various purposes.
You can always perform a search on a string for a specified character or set of characters. This can be done using the strpos() function. On success the function will return the position of the first match in the string while false will be returned on failure of finding the specific characters in string.
For example, consider a string “Hello World, This is PHP Training”. Search for ‘PHP’ word in this string with code strpos(“Hello World, This is PHP Training”,”PHP”) will return 21 as PHP word starts from position 21 in the string. It’s not 22 as you may find upon counting the position manually because the first character position in the string is 0, and not 1.
Another important operation that can be performed on a string is to replace specific characters within the string with new values. This acts similar to the replace all option where you can have a specified character or set of characters be replaced with new value. All the occurrences of the specified characters will be replaced in a single time.
The str_replace() contains three parameters. First, is the search parameter, the value for which you want to replace a new value, second is the replace parameter, the value with which you want to replace the existing value and third is the string on which the operation is to be performed. However str_repalce() can also have a fourth parameter called count which counts the number of replacements done in the string.
$originalString = “blueredgreenyellow”;
echo “Replacements: $j”;
Here the output will be “bluepinkgreenyellow” and 1.
String capitalization in PHP can be done in three ways – converting the entire string to upper case and lower case and making of the first letters of every word in the string to uppercase.
This can be done with the help of strtoupper(), strtolower() and ucwords() functions. These functions will have their effect only on letters but not on numbers. The strtoupper() function will capitalize the entire string, strtolower() will convert all the letters in the string to lower case and ucwords() will capitalize the first letters of every word in the string.
For Example: Consider a string “Hello world 123” for capitalization and look what will be the output you will get from it.
$originalString = “Hello world 123”;
print_r( strtoupper($originalString));// it will display HELLO WORLD 123
print_r( strtolower($originalString));// it will display hello world 123
print_r(ucwords($originalString)); ));// it will display Hello World 123
To learn in detail about the concepts in PHP one should join PHP training classes. Taking up a PHP course that is designed for beginners at a reputed institute can help learn the subject and its essential elements in detail. So for those looking to learn manipulation of strings in PHP should turn out to PHP training in order to gain better understanding of the concept with illustrative practical explanations provided.
TOPS Technologies is a leading Php training institute in Ahmedabad, India with nearly 18 branches spread across various cities in the country. The institute is reputed for its world-class learning infrastructure, methodologies and hence is among the best institutes in country for PHP, .NET, Java, iPhone, Android etc.