Allow Extra Spaces?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

virgil
Forum Commoner
Posts: 59
Joined: Thu Jun 13, 2002 11:43 pm
Location: New York, U.S.

Allow Extra Spaces?

Post by virgil »

Hey PHP's :)

Is there a way to get extra spaces, in text, in a textarea to insert into a database(MySQL) and retrieve it? I'm looking for something that sort of works like nl2br() for spaces.

Thanks for any Ideas

Virgil.... :)
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

what kind of spaces are you talking about?

what kind of form are people filling out?

the spaces automatically go into the database.. if I were to submit this sentence in my database I could display it just like it is.. spaces and all.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

i think i know what you mean.

I was *attempting* to build a forum for my game and it didn't work to well, but in the process i discovered how to add a BBcode type of system to it using eregi_replace() and another thing i noted was the fact that when someone hits enter in the new topic page, it wasn't turning out on the forum page. So i did another eregi_replace() this time instead of inserting smilies or substituting for html code i simply inserted spaces like so:

Code: Select all

// inserting br's into the spaces for the messegebody area

$Pattern = "її]]її]]";
$Replace = "<br>";
$Arrayї"MessegeBody"] = eregi_replace($Pattern, $Replace, $Arrayї"MessegeBody"]);
Note: 2 [[:space:]]'s are needed to tell the differene between a line and a word-word space.
virgil
Forum Commoner
Posts: 59
Joined: Thu Jun 13, 2002 11:43 pm
Location: New York, U.S.

Post by virgil »

Hey again, :)
I mean EXTRA spaces;

Standard form textarea element

Code: Select all

<textarea rows="4" name="message" cols="69"><?=$text?></textarea>

$text="What do_____ you want to do today?"
I can't even show you an example of the text because even this site trims out the extra spaces. The under line represents the Extra spaces.
Actually, its just like this sites textarea! On preview, the form textarea is repopulated with the extra spaces, so $_POST holds on to it some how, but when displayed in html or sent to a database, it's lost.
Should I try to use &-nbsp; ? I thought there might be a function like trim() but in reverse or really notrim() . htmlspecialchar() doen't seem to work for spaces. Any Ideas?

Virgil.... :)
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Use this

Code: Select all

&lt;?php
$string = "Hello, How are you  I am fine";
$finished = str_replace(" ","&amp;nbsp;",$string);
echo $finished;
?&gt;
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

oic, sorry about the confusion :?


But Takuma is right on the dot, that should to the trick.

But with mine, instead of '<br>' you could have just added '&nbsp;'

;)
virgil
Forum Commoner
Posts: 59
Joined: Thu Jun 13, 2002 11:43 pm
Location: New York, U.S.

Post by virgil »

Thanks! :)

That's it. Works like a charm.

Code: Select all

$finished = str_replace(" ","&nbsp;",$string);
...actually Displays "&nbsp" though.

Code: Select all

$finished = str_replace(" ","&-nbsp;",$string);
...will show actual spaces in everthing phpinfo(), Mysql, and textareas.

( I used "&-nbsp" for posts because without the "-" &-nbsp just displays a space. )
(Come to think of it, that's probably what you did. It's 8AM on Sunday and I'm still a little slow.) :roll:

Thanks all for the help
Virgil :D :D :D
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Ah I wondered why you did it...
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post by MattF »

That will work but &nbsp; is a non-breaking space therefore according to the browser it is just one long word therefore if someone posted a long enough thread then the screen would just get stretched horizontally.

EDIT: To make what I said more clear imaginge that &nbsp; is really an invisible _ then you would get this:

this_is_a_really_long_thread_that_some_one_has_written_they_may_be_saying_something_very_important_and_not_trying_to_cause_trouble_but_it_can_negativly_affect_design_templates_as_you_can_see_from_this_post.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

OK :cry:
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

ugh, there should be a block for that :P
virgil
Forum Commoner
Posts: 59
Joined: Thu Jun 13, 2002 11:43 pm
Location: New York, U.S.

Post by virgil »

Sorry PHP's, Bin away...

How bout this?

Code: Select all

$comments = str_replace('--','-&_nbsp;', $comments);

(where "-" is a space (easier to count than"__"))
(&_nbsp is just to allow display of the "no break space code")

This will allow the line breaks and will allow odd and even numbers of
spaces to be displayed. The only draw back is that it's possible to
add "&_nbsp" to the begining of a word causing the
&nbsp;New line to misalign a space....as so...<---





What do you think?

Virgil... :)
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

Code: Select all

for($i=0; $i &lt; strlen($comments); $i++) {
  if($comments&#1111;$i] == " " &amp;&amp; $comments&#1111;$i+1] == " ") {
    echo "&amp;nbsp";
  } else {
    echo $comments&#1111;$i];
  }
}
it's a little sketchy but that's all i could come up with. it will only replace space with nbsp if there is more than 1 consecutive space.
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

also it might make it easier (depending on what your situation is) to add it to a var instead.. in that case just use:

Code: Select all

for($i=0; $i &lt; strlen($comments); $i++) { 
  if($comments&#1111;$i] == " " &amp;&amp; $comments&#1111;$i+1] == " ") { 
    $final .= "&amp;nbsp"; 
  } else { 
    $final .= $comments&#1111;$i]; 
  } 
}

echo $final;
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Of course if the spaces are being used to format the text and you don't need stuff to line wrap why not output the stuff into <pre> tags?

Code: Select all

<pre>Maintain       all spaces   tabs and     line
breaks
   please</pre>
Mac
Post Reply