addslashes() - escapes characters like " ' and ' to make the string safe for using in database queries. The corresponding function is stripslashes()
This is a test 'string' for the CodeHelp pages.
addslashes($string);
This is a test \'string\' for the CodeHelp pages.
stripslashes($string);
This is a test 'string' for the CodeHelp pages.
chop() - remove trailing whitespace from the specified string. In the example, the string is enclosed in [ ] brackets and the background colour is changed to indicate where the whitespace has been removed by chop().
[ This is a test 'string' for the CodeHelp pages. ]
chop($string);
[ This is a test 'string' for the CodeHelp pages.]
explode (and corresponding implode) convert strings into arrays, splitting or joining using delimiter characters - sometimes spaces, or tabs or markers like # or ~. Use explode when you want to work on small portions of the string, for example to change just one portion of the string to capitals (note how the delimiter is removed):
$myarray = explode(" ",$string);
while(list(,$value) = each($myarray)) {
if(ereg("CodeHelp",$value)) {
$value = strtoupper($value);
}
echo $value;
}
Thisisatest'string'fortheCODEHELPpages.
implode takes an array and converts it into a string, inserting the delimiter between values from the array (it puts back what explode takes out). Implode is also useful when opening a file leaves you with an array containing individual lines from the file. Implode can give you one large string that can be easier to search using ereg() than a large array. (The delimiter can be more than a single character, it can also be blank.)
implode("#",$myarray)
###This#is#a#test#'string'#for#the#CodeHelp#pages.######
Unlike chop(), trim() removes whitespace from both ends of the string:
[ This is a test 'string' for the CodeHelp pages. ]
trim($string);
[This is a test 'string' for the CodeHelp pages.]
There are lots of similar compare functions for strings, incuding case insensitive strcasecmp(), regular expressions like ereg() and preg() with wildcards, and the more abstract (including phonetic) comparisons using similar_text(), levenshtein(), metaphone() and soundex(). strcmp() only returns true if the match is exact:
$string1 = "new string";
$string2 = "old string";
$string3 = "new string";
strcmp($string1,$string2) : -1
strcmp($string1,$string3) : 0
ereg("... string",$string1) : 1
ereg("... string",$string2) : 1
ereg("n.. .tring",$string2) :
To match any special characters in functions like ereg() and preg(),
make sure you escape each one : \. \* \?. For more on wildcard characters,
see 6 - Tainted input data. This is just a quick reminder.
. matches any character except a newline,
* preceding character or group may be omitted or appear once, twice or more.
? preceding character or group may be omitted or may appear once but not twice or more.
+ preceding character or group must be present once but appear twice or more.
^ the beginning of the string.
$ the end of the string.
[abc] any one of the characters a, b, or c can appear once.
[^abc] any character except a, b or c can appear once.
[a-z] one lowercase alphabet character.
(abc) the phrase 'abc' may appear once - bac, ab, c or aBc do not match.
htmlspecialchars() makes all the < and > characters safe. < becomes <, > becomes > and & becomes &. This comment line is present as <!-- comment & --> in the HTML source.
htmlspecialchars($string); : <!-- comment & -->
As an alternative to ereg_replace("<.*>","",$myinput), (which can strip out more than you might expect), PHP provides the strip_tags() function that will remove all HTML and PHP tags from a string. This is important if you are accepting text input directly into the PHP script - it could otherwise allow visitors to corrupt your script by entering malicious text.
strip_tags(<!-- comment & --> more content <?php active script?>);
: more content
Other conversions: (Note that the string itself remains unchanged between each of these demonstration commands.)
strtoupper($string); : THIS IS A TEST 'STRING' FOR THE CODEHELP PAGES. strtolower($string); : this is a test 'string' for the codehelp pages. ucfirst($string); : This is a test 'string' for the CodeHelp pages. ucwords($string); : This Is A Test 'string' For The CodeHelp Pages. wordwrap($string,8,"\n"); : This is a test 'string' for the CodeHelp pages.
The final conversion requires a change to the string. These two sentences are now the string.
nl2br($string); : The final conversion requires a change
to the string. These two sentences
are now the string.
nl2br() changed the newlines in the source code into <br> tags. This can be useful in processing some forms or converting email messages into HTML archives.
This is part of www.codehelp.co.uk Copyright © 1998-2004 Neil Williams
See the file about.html for
copying conditions.