PHP Lesson 19 - string functions

Started by Parham, May 18, 2005, 01:44:20 PM

Previous topic - Next topic

Parham

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.


$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.


$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.


$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.

[Unknown]

Actually, chop is an alias for rtrim in PHP.  See:

http://www.php.net/function.chop

Other useful functions include substr, strpos, strtok, htmlspecialchars, implode, explode, md5/sha1, strtr and/or str_replace, stripslashes, strtoupper, strtolower, etc.  Of course, there's just no replacement to looking at these functions documentation ;).

-[Unknown]

Parham

#2
my mistake, i'll correct that right now.  i could have sworn it worked like perl's chop() at one point.  my mistake though.

Unknown is right, there's no substitute for the functions documentation on the PHP website.

[Unknown]

Actually, I think it may have worked like Perl's chop, and they changed it.  Not sure...

-[Unknown]

hryan

Hi!

How to eliminate newLine from a string and get only one_row_simbolString?
---
i.e.

$s='fsdf
sdfdsf
sfsdfdf'."ss\n\n";

to get $s=='fsdfsdfdsfsfsdfdfss'
--

thanks!

Saleh

#5
echo str_replace(array("\n""\r"), array(''''), $s);

should remove the newlines.

We don't need a reason to help people

absolute beginner

Hi everyone!!! This is great tutorial!
I have one excellent example of string funtion. It's more like problem. This is a part of html verson of script:
$uusirivi='<div class="boxblue">'.strip_tags($nimi) . ':</div><div class="boxgray"> ' . $viesti . '</div><br/>
';//HERE IS OUR PROBLEM
  $tiedosto = fopen('chat.txt', 'r+');
  $kolmas=file_get_contents ('chat.txt');
  $toinen="$uusirivi $kolmas";
    fputs($tiedosto, $toinen);
if (filesize('chat.txt')>1000000)
  {ftruncate ($tiedosto,1000000);}
    fclose($tiedosto);
  $viestik=$viesti;
  $nimik=$nimi;
  $viesti ='';
}

This is a part of wml version of script:

  $uusirivi ='<small><b>'.strip_tags($nimi) . '</b></small>: ' . $viesti . '<br/>
';//WATCH THIS LINE
  $tiedosto = fopen('chat.txt', 'r+');
  $kolmas=file_get_contents ('chat.txt');
  $toinen="$uusirivi $kolmas";
    fputs($tiedosto, $toinen);
if (filesize('chat.txt')>1000000)
  {ftruncate ($tiedosto,1000000);}
    fclose($tiedosto);
  $viestik=$viesti;
  $nimik=$nimi;
  $viesti = '';

Now, as you can see I wanted to use function strip_tags (because wml doesn't read div tags, but
it is not working here, because all content (e.g. <div class=something>john</div> is coming from cat.txt file!
How can I work with strip_tag to leave div tags in html version
and to remove them in wml?
Great example, yes?

Parham

Take a look at the php documentation on strip_tags... replace
strip_tags($nimi)
strip_tags($nimi,'<div>')

Advertisement: