Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I change line breaks in entered data into html code "< br >&qu

Status
Not open for further replies.

Guest_imported

New member
Joined
Jan 1, 1970
Messages
0
I am a real beginner to this. I use perl for handling forms that post poetry and the like to my websight. What I would really like to do is turn the linke breaks (returns) on the entered data into html line breaks. Is this possible. As I said, I am a real beginner so please be as simple as possible.
Also, Is there a way of getting rid of spaces so that A A A entered on the form would turn into AAA ?

Hope you can help because I am stumped.

yellow mongoose
 

I had this problem when taking data from a mysql database. Try something like:
[tt]
while (<FILEHANDLE>) {
s/\n/<br>\n/g;
print;
}
[/tt]
this will replace all newlines with a html linebreak and a newline (the newline to make the html code prettier). You can modify it for your purposes.

Will.
fortytwo
will@hellacool.co.uk
 
For the spacing part use;

$variable =~ s// /g; # This will remove all spaces and replace with nothing (thus removing the space)

That should work for your second question. As for your second on, I am not sure. I think a return charachter in \r, but maybe people who are a bit more experienced will know for definate ;-)

$variable =~ s/\r/\<BR\>/g; # Prettry sure you need to escape the < and > signs ;-)

Hope that helps

Andy
 
That's great. Thanks both of you for the help. I'll try it out and see how I go. This will really help me.
Cheers to both of you.
 
Andy,
Actually I have figured out the problem with the spaces, but I can't seem to reverse the affect. Is there a way of doing this so that after the field has appeared once without spaces, it can later appear as it was typed, with the spaces?

If you could help me with this I would be grateful.

Thanks for the other piece of coding, it works an absolute treat. That is a much appreciated help. Cheers.

Benjamin Jackson
 
The code to replace spaces with nothing is backwards, it should be
Code:
$var =~ s/ //g;
And you don't need to escape the < or > in the other regex, but he's on the right track with the \r. To be even clearer, use \x0a (carriage return) or \x0d (linefeed), like this:
Code:
$var =~ s/\x0a|\x0d/<br>\n/g;
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
You could do the replacement on a copy of the variable, so:
[tt]
$copy_of_variable = $variable;
$copy_of_variable =~ s/\r/\<BR\>/g;
[/tt]
so now $variable has not changed.

Will
fortytwo
will@hellacool.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top