Home Tutorial PHP Lesson 19 - String functions



MSN Random Online

Lesson 19 - String functions PDF Print E-mail
Written by Administrator   
Thursday, 15 February 2007 17:49
I think I should have started with string functions before I gave the regular expressions lesson, but I think it's about time for this lesson.ย? String functions are exactly that, functions which manipulate a string (consisting of letters and numbers).ย? The entire list of them can be found over at http://www.php.net/manual/en/ref.strings.php.

Starting from the top of the list, there are some main functions I want to go over and cover because they are functions you will find yourself using often.ย? The first function addslashes() will come in VERY handy when working with databases.ย? Remember that when adding anything to a database, the quotes should be escaped (for example, "Parham\'s database").ย? This function escapes special characters in the unescaped string, making sure it is safe to place in a mysql_query() function.ย? This function is not foolproof, but it will do for quick jobs.ย? Remember that if you have the magic_quotes_gpc directive set to "1" (use get_magic_quotes_gpc() to find out), and taking information right from the _GET/_POST/_COOKIE superglobals, then PHP will automatically escape special characters for you.

Code:
$string = "Parham's variable aren't safe to use in a database query";
$string = addslashes($string);
//now you can use $string in a query

This function is however very generic.ย? PHP also provides safer functions for different database types.ย? mysql_real_escape_string() and pg_escape_string() should be used more often than this generic function.

chop(), trim(), ltrim(), rtrim() are all functions which strip whitespace (or other characters) from a string.ย? trim(), ltrim(), and rtrim() remove only whitespaces, \n, \r, , \t, \0, \x0B.ย? ltrim() only trims the left side of a string, and rtrim() only the right side.ย? chop() works exactly like rtrim().ย? Note that this function doesn't work similar to the Perl equivalent which removes the rightmost character regardless of what it is.ย? This function only removes whitespaces and other similar characters.

Code:
$string = 'ย? ย? ย?testย? ย? ย?';
$string = trim($string);
//$string will now only be 'test'

When taking in user input, it's always good to know how much a user is trying to submit.ย? For example, you'd like to be able to know that a user is submitting a string which is 100 characters, because your database column can only handle 40 characters.ย? On submission of any variable, when concerned about the length of string input, use the strlen() function to check length.ย? This function simply returns the number of characters in a string.

Code:
$string = 'testing 123, testing';
echo strlen($string); //will print 20


If you're scared of user input, there is another function you may want to consider.ย? strip_tags() will remove any html tags a user might have put into a string.ย? This function comes in very handy when, for example, when user input it displayed right away on a page, like in a guestbook or message board posts.

The point I'm trying to get across here is that there are functions for most of the normal things you might want to do to strings.ย? PHP's library of functions provides most of these for you to use safely.ย? There are many more functions you may want to consider using.ย? Search through the string function list to see what you can do.

This lesson was fairly short, because the string functions are straight-forward to understand.ย? If you have any particular questions though, feel free to ask.ย? I can answer more specific questions a lot better.

ย?

Test Code php

xxxxxxxxxx
 
 
Last Updated on Thursday, 15 January 2009 04:46