Jump to content

Remove spaces


napsternapster

Recommended Posts

morning Developers,

 

I'm currently finalizing a project today but the problem I came across is the number of spaces when a user entering data.I need a code to remove trailing and spaces between words like the following example.

 

# = number of spaces

 

##Hellow#####world##

 

Want to Accomplish:

Remove the spaces(#) before Hellow and after world,

Eliminate all unnecessary spaces(#) between the words leaving one space(#) between the two worlds.

 

Thanking you in advice.

 

Link to comment
https://forums.phpfreaks.com/topic/177880-remove-spaces/
Share on other sites

Hi

 

Something like this:-

 

function getRidOfSpaces(InStr)
{
InStr = String.trim(InStr);
var lastLength = InStr.length + 1;
while(InStr.length < lastLength)
{
	lastLength = InStr.length;
	InStr = InStr.replace('  ',' ');
}
return InStr;
}

 

This just loops through converting 2 spaces to a single space until the length doesn't change doing this.

 

All the best

 

Keith

Link to comment
https://forums.phpfreaks.com/topic/177880-remove-spaces/#findComment-937999
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.