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

Post Reply
goodwinpro
Forum Newbie
Posts: 9
Joined: Wed Mar 19, 2008 4:51 pm

Can't get carriage return to work

Post by goodwinpro »

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)", "<br />", $copy); //replaces coded breaks with hard breaks
$copy = str_replace("'",'&rsquo;',$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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Can't get carriage return to work

Post by Christopher »

What happens if you do:

Code: Select all

$copy = ereg_replace('(\r\n|\n|\r)', "<br />", $copy);
(#10850)
goodwinpro
Forum Newbie
Posts: 9
Joined: Wed Mar 19, 2008 4:51 pm

Re: Can't get carriage return to work

Post by goodwinpro »

$copy = ereg_replace('(\r\n|\n|\r)', "<br />", $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
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: Can't get carriage return to work

Post by GeertDD »

Note that for the regexes mentioned above, you could equally use the faster str_replace function.
Post Reply