Can't get carriage return to work

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Can't get carriage return to work

Postby goodwinpro » Wed Mar 19, 2008 5:01 pm

I'm copying chunks of text from pdf files into..
(1)HTML KIT - for minor formatting. Then gets copied to...
(2) A text area in a form which then gets....
(3) Inserted into a Mysql Database.

The PDF document obviously puts in some sort of line breaks to wrap the text. But I can't get rid of them when pulling the information from the database. And I don't want to have to manually 'backspace' all the line breaks out as I'm putting the text into the database.

Here's my code for cleaning up the text as it's being displayed.

$copy = ereg_replace("(\r\n|\n|\r)", "
", $copy); //replaces coded breaks with hard breaks
$copy = str_replace("'",'’',$copy); // takes out apostrophes
$copy = str_replace(chr(13)," ",$copy); // deletes carriage returns
$copy = str_replace(chr(10)," ",$copy); // deletes carriage returns

I would assume that this would do it. But it still doesn't work. My carriage returns still show up.

Using PHP5 on a windows server 2003

Does anyone have any suggestions?
goodwinpro
Forum Newbie
 
Posts: 9
Joined: Wed Mar 19, 2008 4:51 pm

Re: Can't get carriage return to work

Postby Christopher » Wed Mar 19, 2008 6:44 pm

What happens if you do:
Syntax: [ Download ] [ Hide ]
$copy = ereg_replace('(\r\n|\n|\r)', "<br />", $copy);
(#10850)
User avatar
Christopher
Site Administrator
 
Posts: 10514
Joined: Wed Aug 25, 2004 7:54 pm
Location: Portland, Oregon, US

Re: Can't get carriage return to work

Postby goodwinpro » Thu Mar 20, 2008 1:12 pm

$copy = ereg_replace('(\r\n|\n|\r)', "
", $copy);
This is simply replacing the double quotes with single quotes around the first part of the argument.

It looks like that actually physically takes out all of the R's and N's in the text and replaces them with a <br/>. (See example below)

However, thanks to your suggestion. I played around with it and discovered that my first line is actually adding the line breaks. So if I change that to $copy = ereg_replace("(\r\n|\n|\r)", "", $copy); it works as I am wanting it to. So it appears that it's my inexperience at PHP that's getting in the way of my progress.

So thanks for your help. I think it's solved.


Example:
ai
es’ Row (the fi
st was o
the
ea
/>South Side a
ou
d 18th St
eet a
d P
ai
ie Ave
ue, whe
e a few old ma
sio
s />still exist) a
d
emai
s home to ma
y well-to-do Chicagoa
goodwinpro
Forum Newbie
 
Posts: 9
Joined: Wed Mar 19, 2008 4:51 pm

Re: Can't get carriage return to work

Postby GeertDD » Fri Mar 21, 2008 2:01 am

Note that for the regexes mentioned above, you could equally use the faster str_replace function.
User avatar
GeertDD
Forum Contributor
 
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium


Return to Regex

Who is online

Users browsing this forum: No registered users and 1 guest