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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Escaping spaces but allowing text wrap

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi peeps,

I'm currently using the following code..
Code:
# Replace Spaces With HTML
$page[1]{'PageText'} =~ s/\s\s/\  /g;

but it is not fully replacing spaces relative to the way they were types in the text box.

I was just using one \s but that meant the text did not wrap in the DIV tag because there were no spaces in the string of text

ie.
Code:
this is a line of text that wont wrap in a table td tag

there needs to be a space between words for the auto wrap to work, but where more than one space is in a row, to make it show in HTML you need to use the escape code.

how do i solve this dilema?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
but it is not fully replacing spaces relative to the way they were types in the text box.

Can you explain more about what that sentence means please? What output were you getting and how did it differ from what you were expecting?
 
hey ishnid nice to see you.

ok this is what is entered.

Code:
   ||
   ||
   ||
   ||
   ||
   ||
   ||
\  ||  /
 \ || /
  \||/

this is what I get when displayed

Code:
    ||
    ||
    ||
    ||
    ||
    ||
    ||
\  ||  /
\  ||  /
  \||/

and any chance you can help with my other post... thread219-1106205

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
it looks to me that you might need to use some sort of positive (or negative) lookahead assertion in the regex - i.e. only replace the space if there is a space to the right of it

Code:
[b]#!/usr/bin/perl[/b]

$_ = 'this    is a string       of   text  with    random      spaces in  it';

s/\s(?= )/ /g;

print;

if i have understood correctly - and i have not made a balls-up of it!

I'm sure the more savvy MVP's (and yourself) will set me straight if i have missed the point


Kind Regards
Duncan
 
if this meant when displayed as HTML it looks just like you typed it and it also means there are actual spaces in the string so it word wraps then yes!

i'll try it and let you know

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
absolutely - this script will replace the spaces with   - but will leave one space for your wrapping

... i hope


Kind Regards
Duncan
 
Duncan's looks like exactly what you're looking for. I'll have a stab at explaining why.

I suspect your original attempt works fine for even numbers of consecutive spaces but not odd numbers. When you use the /g option in a regexp, it starts each replacement at the point it left off after the last one. That means that with four spaces, it firstly performs the replacement on the first two. Then it goes to try again, beginning after it left off (i.e. on the third space). It finds a space with another space after it so it does the replacement ok.

With three spaces, it again successfully performs the first replacement. Again it starts at the third space for the second replacement and finds no more spaces to convert so ignores it, even though there are still 2 consecutive spaces in the string, as the second character is still a space too.

As a purely TIMTOWDTI excercise (since I like dunc's solution and would use that), the following replacements will probably work too:
Code:
1 while( $page[1]{'PageText'} =~ s/\s\s/\  / );
$page[1]{'PageText'} =~ s/\s\s/\  /g;
 
I get the same result with that code duncan.

my train of thought with mine though ishnid is odds didn't matter, although I put the   the wrong way round to the other space.

but as you show it should work
Code:
s/\s\s/\  /g;

that way any two spaces next to each other get a space then   so a third space would make

[space] [space]

that should still show as three spaces in HTML shouldn't it?



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
o.k.

what happens if you try this?:-

Code:
s/\s{2}/  /g;


Kind Regards
Duncan
 
that gives me
Code:
  ||
  ||
  ||
  ||
  ||
  ||
  ||
\  ||  /
\ || /
 \||/

i don't think it really matters too much, it's just something I noticed when mucking around putting crap in a text box to cause an overflow so i could see the scrollbars appear rather than the page expand in length.

but it is obviously a major idiosyncrasy and a fix would be nice.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
rharsh - WORD WRAP!

dunc
Code:
&nbsp; ||<br> &nbsp; ||<br> &nbsp; ||<br> &nbsp; ||<br> &nbsp; ||<br> &nbsp; ||<br> &nbsp; ||<br>\ &nbsp;|| &nbsp;/<br> \ || /<br> &nbsp;\||/<br>

does that help

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Hi 1DMF

That arrow is a bit knackered... 3rd from bottom is offset to the right

Also, i did not realise there were &nbsp; in there already!?


Kind Regards
Duncan
 
why aren't you just using the <pre></pre> tags to display text in it's natural formatting?
 
Dunc I think you're missunderstanding, the nbsp; is what your PERL substitute has added the arrow is entered via a <TEXTAREA> html tag, saved in the SQL (as is) and then formatted for redisplay, like the \r\n i have to do to place the <br> in the code etc...

Kevin does <pre> format all [hard returns], [spaces] etc...

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
No good Kevin, although the arrow aligns correct, all the CSS styles in the DIV tag surrounding the text is completely ignored.

Can you place the style="CSS Stuff" in a <pre> tag ?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top