...

View Full Version : adding spaces to a text string



Taipan
04-01-2004, 12:27 AM
I am reworking an existing web site and part of it involves taking a members name, converting it to all caps and then double spacing the letters. I can display the name in caps using strtoupper() but I have no idea how to add a space between each letter. As it is a 2 word name I would need to double space between the names so easiest way is to place a forced space   between each letter in the name. Is there a simple way I can do this?

Any help would be appreciated.

sad69
04-01-2004, 01:12 AM
Something like this perhaps?



function spaceName($member_name) {
$name_array = str_split($member_name); //splits $member_name characters
$spaced_name = "";
for($i = 0; i < sizeof($name_array); i++) {
$spaced_name .= $name_array[$i]." ";
}

return $spaced_name;
}


I think that should work... let me know.

Sadiq.

sidney
04-01-2004, 01:47 AM
and for those with out php 5


<?php
function spaceName($member_name) {
for($i=0;$i<strlen($member_name);$i++)
{
$name_array[]=substr($member_name,$i,1);
}
$spaced_name = implode(" ",$name_array);
return $spaced_name;
}
echo spaceName(tommy);
?>

Taipan
04-01-2004, 02:30 AM
Thanks sidney,

I am getting an error - Parse error: parse error, expecting `')''


function spaceName($member_name) {
$name_array = str_split($member_name); //splits $member_name characters
$spaced_name = "";
for($i = 0; i < sizeof($name_array); i++) { error is on this line
$spaced_name .= $name_array[$i]." ";
}

return $spaced_name;
}


Any ideas?

sad69
04-01-2004, 02:33 AM
Sorry, I forgot the $...



function spaceName($member_name) {
$name_array = str_split($member_name); //splits $member_name characters
$spaced_name = "";
for($i = 0; $i < sizeof($name_array); $i++) { error is on this line
$spaced_name .= $name_array[$i]." ";
}

return $spaced_name;
}

Taipan
04-01-2004, 03:06 AM
thanks :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum