Advertisement:

Author Topic: PHP Lesson 19 - string functions  (Read 25735 times)

Offline Parham

  • SMF Friend
  • Sophist Member
  • *
  • Posts: 1,317
  • Gender: Male
  • #!/usr/bin/perl
    • Online Node
PHP Lesson 19 - string functions
« on: May 18, 2005, 01:44:20 PM »
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: [Select]
$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: [Select]
$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: [Select]
$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.
« Last Edit: May 18, 2005, 03:57:49 PM by Parham »

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: PHP Lesson 19 - string functions
« Reply #1 on: May 18, 2005, 02:48:11 PM »
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]

Offline Parham

  • SMF Friend
  • Sophist Member
  • *
  • Posts: 1,317
  • Gender: Male
  • #!/usr/bin/perl
    • Online Node
Re: PHP Lesson 19 - string functions
« Reply #2 on: May 18, 2005, 03:56:35 PM »
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.
« Last Edit: May 18, 2005, 05:34:18 PM by [Unknown] »

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: PHP Lesson 19 - string functions
« Reply #3 on: May 18, 2005, 05:34:50 PM »
Actually, I think it may have worked like Perl's chop, and they changed it.  Not sure...

-[Unknown]

Offline hryan

  • Newbie
  • *
  • Posts: 3
Re: PHP Lesson 19 - string functions
« Reply #4 on: September 29, 2005, 02:15:11 AM »
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!

Offline Saleh

  • Sophist Member
  • *****
  • Posts: 1,326
  • Gender: Male
  • Claymore Fan
    • PHP Simplicity
Re: PHP Lesson 19 - string functions
« Reply #5 on: October 10, 2005, 09:58:58 AM »
echo str_replace(array("\n""\r"), array('', ''), $s);

should remove the newlines.
« Last Edit: October 10, 2005, 10:05:39 AM by Saleh »

We don't need a reason to help people

Offline absolute beginner

  • Newbie
  • *
  • Posts: 2
Re: PHP Lesson 19 - string functions
« Reply #6 on: May 22, 2006, 02:12:30 PM »
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:
Code: [Select]
$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:
Code: [Select]
  $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.
Code: [Select]
<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?

Offline Parham

  • SMF Friend
  • Sophist Member
  • *
  • Posts: 1,317
  • Gender: Male
  • #!/usr/bin/perl
    • Online Node
Re: PHP Lesson 19 - string functions
« Reply #7 on: May 22, 2006, 08:04:27 PM »
Take a look at the php documentation on strip_tags... replace
Code: [Select]
strip_tags($nimi)
Code: [Select]
strip_tags($nimi,'<div>')