PHP String Functions
PHP is equally powerful than any other server side scripting languages in handling strings. We will experiment with some of the string functions of PHP with some examples. There are powerful functions like regular expressions to manage complex string handling requirements. Here are some of them.
|preg_replace()||regular expression string replacement|
To display html tags on web pages we have to use htmlspcialchars function
We can use md5 php function to encrypt strings to make it secure.
We can use nl2br function to take care of line breaks while displaying in web pages.
We can generate random string by using rand function
We can use strip_tags function to remove html tags from a string and extract the text part.
All presence of a searching string can be replaced by using str_replace function, this is case sensitive
We can use case in-sensitive string replacement by using str_ireplace function.
By using strlen function in PHP we can calculate length of the string
We can reverse a string by using strrev function
Searching for a string inside main string
We can create an array by breaking a string using split command
We can generate a substring from our main string by using substr command
Remove chars from end of the string by using length and sub string functions
Calculating number of times a substring is present inside a main string
Specify number of times a string has to repeat
We can change from lower to upper and upper to lower case chars of a string
PHP string comparison function
A case sensitive string comparison can be done by using strcmp function
We can pad strings from left or right or from both sides.
We can remove blank space from left or right of a string
The first character of sting can be made upper case by using ucwords
Collecting array of records from CSV file by using file pointer.
Storing data from array or database table in a CSV file.
- strpos() case-sensitive search returns position of first occurence
- stripos() case-insensitive search returns position of first occurence
- strrpos() case-sensitive search returns position of last occurence
- strripos() case-insensitive search returns position of last occurence