Problem with using blank spaces as padding

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

Post Reply
ulterior
Forum Newbie
Posts: 2
Joined: Mon Nov 24, 2008 3:50 pm

Problem with using blank spaces as padding

Post by ulterior »

Hi, please bear with me - this may be an insanely simple problem, but its one that has stumped me so far.

So I'm developing a webapp, it reads results from a MySQL db, formats them into a string, publishes this as xml which is then sent via sms message. However the results need to be divided up so that sentences arent split across text messages - so my code adds x amount of underscore characters after a series of sentences in order to fill up the remaining 140 characters of a sms message (which then allows the next sentences to begin at the start of the next message).

So its sort of like "Sentence 1. Sentence 2 is long_____________________Sentence 3. Sentence 4. Sentence 5.______" (except across 140 characters). As produced by:

Code: Select all

                        $q = 0;
                            while ($q <= $tofill)
                            {
                                $blank3 = $blank3 . "_";
                                $q=$q+1;
                            }
Anyway, my problem is that instead of underscores I want to use blank spaces. Again this is probably just a vast oversight on my behalf, but I havent been able to find a way to produce concurrent blank spaces in php. If I change

"$blank3 = $blank3 . "_";"

to

$blank3 = $blank3 . " ";

the spaces are collapsed, so instead of 16 underscores I get a single space. Is this just a stupid oversight on my behalf? Surely there is a way of putting multiple spaces into a string....?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Problem with using blank spaces as padding

Post by onion2k »

Are you viewing the output in a web browser in order to check it works?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Problem with using blank spaces as padding

Post by John Cartwright »

Should be working, otherwise try using the html entity for whitespace instead. Secondly, that is one nasty loop! Can be simplified with to following.

Code: Select all

echo str_pad($input, 20, "&nbsp;", STR_PAD_LEFT); //spaces
echo str_pad($input, 20, "_", STR_PAD_LEFT); //underscores
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Problem with using blank spaces as padding

Post by RobertGonzalez »

Browsers ignore whitespace. No matter the number of spaces you put into your markup they will always render as one space.

Trying using the &nbsp; character between spaces. Or better yet, try controlling the look of the output with CSS instead of through markup.
ulterior
Forum Newbie
Posts: 2
Joined: Mon Nov 24, 2008 3:50 pm

Re: Problem with using blank spaces as padding

Post by ulterior »

Thanks for the quick reply. I'm still familiarizing myself with all the php functions/classes, so I apologize for the inefficient coding.

I'd tried using the html entry for whitespace before, but it doesnt play so well with the xml format I think.

I'll check with my supplier as to whether their side of things reads the browser output or the source.

Thanks for the help.
Post Reply