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

Insert Space Char into a line of Text 1

Status
Not open for further replies.

likelylad

IS-IT--Management
Joined
Jul 4, 2002
Messages
388
Location
GB
If I have the following

$spaces=3;
$string="ÀyÀuppaÀsdfer";

Using $spaces,how do I insert that number of blanks spaces at the end of $string so that I get

$string="ÀyÀuppaÀsdfer ";
 
A simple way is this:

Code:
$spaces=3;
$string="ÀyÀuppaÀsdfer";

for($i=0;$i<$spaces;$i++)
    $string.=' ';
 
Or even:

Code:
$spaces=3;
$string=&quot;ÀyÀuppaÀsdfer&quot;;

$string=str_pad ($string, $spaces, ' ',  STR_PAD_RIGHT)
 
I now I have given a star on this one.
Assumed it was going to work.

My output is still the original string
 
How are you looking at the output?
You need to be aware that HTML is not whitspace specific. So, in a browser the output of 50 spaces after a word is the same as one space.
If you want to pad the string for HTML purposes you need to use &nbsp;
 
That was meant to be
Code:
&nbsp;
I assume?
 
Damn thing, & nbsp without the gap after the &
 
I am glad I'm not the only one that falls into the same trap. The darn thing is that the preview showed the non-breaking-space which I constructed with an HTML entity for the ampersand...
 
don't forget the ; at the end of a nbsp, IE will let you get away with it sometimes, but Netscape won't.

-Rob
 
thanks for the help.

Yep,was using a browser

Have it working properly now.

Thanks Again
 
Correct example should be:

Code:
$spaces=3;
$string=&quot;ÀyÀuppaÀsdfer&quot;;

print str_pad ($string, strlen($string)+$spaces);

(as the pad length argument in str_pad includes the length of the original string)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top