View Full Version : php newbie-strip carriage returns from forms
loki
03-06-2004, 15:44/03:44PM
i'm using a fairly simply php script to parse the contents of a form, and send the contents
1. via email to me and
2. to a txt file that i can later import into a MSAccess db.
but the carriage returns within the field data(not the one at the end of each field) is causing hell when i try to import the txt file into access.
is there a simple code to get rid of all carriage returns except the last one?
ps not so much a newbie, as a cutter and paster.
chrishirst
03-06-2004, 19:24/07:24PM
strip all the carriage returns out replace them with a space , then tag one on to the end
$str = str_replace(chr(13),' ',$str);
$str .= chr(13);
you may need to strip out the newline char (chr(10) as well. just add in an extra str_replace() line but put in a empty string for the replace.
loki
04-06-2004, 07:41/07:41AM
so it would look like this:
$str = str_replace(chr(13),' ',$str);
$str .= chr(13);
$str = str_replace(chr(10),' ',$str);
$str .= chr(10);
chrishirst
04-06-2004, 07:56/07:56AM
more like this (no space between the single quotes)
$str = str_replace(chr(13),' ',$str);
$str = str_replace(chr(10),'',$str);
$str .= chr(13);
$str .= chr(10);
and you may not need the linefeed (chr(10)) added on to the end assuming you are running windows.
loki
04-06-2004, 08:10/08:10AM
have tried above but carriage returns still cause the db to skip to the next field. attached is how i've written the script with your suggestions (may have got this wrong, am blundering in the dark here).
loki
04-06-2004, 08:11/08:11AM
oops, here's the script renamed from .php
chrishirst
04-06-2004, 13:23/01:23PM
well it should work ok apart from one small point, you will need to replace my example variable ($str) with then one you actually need stripping.
loki
04-06-2004, 14:46/02:46PM
(my last 2 emails were posted before i read your last email)
chris,
not really sure where you're going.
my form has about 40 fields, and i'd like to use it in others that are equally lengthy too.
is there a straightforward way of getting php to strip the carriage returns from all fields(in one go), rather than naming them one by one?
chrishirst
04-06-2004, 15:10/03:10PM
make a function to do the char stripping.
function StripCR ($str)
{
$str = str_replace(chr(13),' ',$str);
$str = str_replace(chr(10),'',$str);
$str .= chr(13);
$str .= chr(10);
return $str;
}
then call it as any other function
$message = StripCR($message);
If you put it into an included file then it will be available as library code.
loki
15-06-2004, 14:10/02:10PM
this is over my head.
i know this is an imposition but would you to add thefunction to do the char stripping into my script for me?
appreciate it.
WebSavvy
15-06-2004, 15:54/03:54PM
Whitespace in forms and line breaks are major problems in MySQL and take up LOTS of extra space. You can do as Chris suggested (it does work) and you can also run a query on the db like this to remove junk:
UPDATE table_name SET field_name = replace(field_name, '\n\n', '');
Where is table_name replace with the name of your table
Where is field_name replace with the name of your field (both instances of field_name need to be exactly the same)
If you have questions, just ask. :)
loki
15-06-2004, 16:46/04:46PM
i'm not using MySQL, i'm taking the txt file and manually adding it to a MSAccess db (at the end of each month).
am also hoping chris has the time to tweak my script for me ;-)
craig
WebSavvy
15-06-2004, 16:53/04:53PM
LOL! OK, well ... then I'm no help to you. ;) I don't know anything about MSAccess. I've only used SQL bases with PHP or Perl.
:D
chrishirst
16-06-2004, 05:30/05:30AM
Ok mostly it's all done here, just a matter of putting it together.
copy & paste this code into a new file (no html etc). Save it as a php file, say "functions.php"
<?php
function StripCR ($str)
{
$str = str_replace(chr(13),' ',$str);
$str = str_replace(chr(10),'',$str);
$str .= chr(13);
$str .= chr(10);
return $str;
}
?>
any page where you need to use this function put this code in the document before you need to make the call
<?php
include('functions.php');
?>
when you need to use the function it's called just as the predefined functions are used
$address = StripCR($_REQUEST('address'));
HTH
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.