PHP play with strings
Last update on: 10-09-2008You will discover in this course how to manipulate strings and the list of functions.
List of functions:
| Function | Description |
| strtolower() | rewrite in lowercase |
| strtoupper() | rewrite in uppercase |
| ucfirst() | rewrite in uppercase the initial |
| nl2br() | Replace the \n by <br> for display |
| htmlspecialshars() | Converts html characters |
| addslashes() | add an \ (slashe) before the special characters |
| stripslashes() | take off the slashes(\) |
| ltrim() | Deletes the original spaces |
| trim() | take off the first and the last spaces |
the replace function:
//give a simple sentence $sentence = "PHP is a shareware scripting language"; //we print the sentence print $sentence.'<br>'; //we will replace shareware by free $sentence = str_replace("shareware","free",$sentence) ; //we display the sentence after change print $sentence; Output PHP is a shareware scripting language PHP is a free scripting language
Searching:
//the sentence $string = "PHP is a free scripting language"; //we check if the word free is in the sentence if( ereg('free',$string)) { //if yes we print ... print 'Yes the word is in the sentence'; } //else else{ print '"free" is not in the sentence'; } Output Yes the word is in the sentence
Splitting:
$string = "PHP is a free scripting language"; //we use the split() function to split the sentence by spaces available $result = split(" ",$string); //Caution: the result will be in an array form print $result['3'].'<br>'.$result['5']; Output free language
verify if a variable is empty:
//empty verify if the variable is defined if(empty($variable_to_verify)) { print "the variable is empty"; //error message variable empty } else{ print "it is ok"; //the variable is not empty } #-- the opposite of empty() is isset() //isset verify if the variable is not empty if( isset($variable_to_verify)) { print "ok the variable is set"; } else{ print "the variable is empty"; }
PHP and MySQL's lessons:
Introduction To PHPGet Started With PHP
PHP Variables
PHP Variables Of Environment
PHP Conditions
PHP Looping
PHP Cookies
PHP Working With Dates
PHP Working With Arrays
PHP Working With Files
PHP Play With Strings
PHP And Forms
Send Emails With PHP
The Include Statement
Get Started With MySQL
MySQL Update And Delete
The WHERE Clause
MySQL Functions
Guestbook Script
Websites Directory Script
Multiple Pages With PHP
Create Your Forum With Php

