Strip white space from string

We have been doing some work with PHP and Opto Control.
We send a URL to the PHP using a com handle.
If there are any spaces in the URL, it breaks the URL, so we need to strip it out.
In our optoscript, we ‘wash’ all our strings with this little bit of code to strip out all spaces that might be there…


while (GetNthCharacter(temp_string, 0) == ' ')
  GetSubstring(temp_string, 1, GetStringLength(temp_string), temp_string);
wend

What its doing is using the string ‘temp_string’ and looking to see if anywhere, starting at the front of the string (0), is equal to ’ ’ i.e. a space.
If it is, just pull it out and put the rest of the string back together.
Loop through the whole string looking for spaces.

Result; ‘this is a test’ becomes ‘thisisatest’.
Of course you can also use it as a kind of find and replace, so you could other things with it as well.

Code on!

ben
here is a snippet of code i’ve been using to encode strings to be sent to a web server for processing. Its kinda along the same lines as the snippet above. I had trouble inserting data into a database and thought that it’d be just as easy to encode the string on the controller rather than muck around on the server side (and to ensure the url isn’t broken). although i am using with PAC Control, i can see no reason why it cannot be used with OptoControl also (I will be implementing on an M4 soon enough).


//ok very simple process here. we need to loop through each char in the string and if we find
//a character deemed as a reserved or unsafe character then
//we then insert into the string the encoded string (ie space would become %20).
//the string to be encoded is sToBeEncoded and the result goes into sEncodedString
sTempStr="";  //clear the temp string used to house the encoded string
intSubStrLen=0;  //this is what position we start when disecting the string
for x = 0 to intStrLength step 1
  intChar = GetNthCharacter(sToBeEncoded, x);
  if ((intChar==36)or(intChar==38)or(intChar==43)or(intChar==44)or(intChar==47)or(intChar==58)or(intChar==59)or(intChar==63)or(intChar==64)
  or(intChar==32)or(intChar==34)or(intChar==60)or(intChar==62)or(intChar==35)or(intChar==37)) then
    //other characters that can be included are: 123,124,125,92,94,126,91,93 and 96
    GetSubstring(sToBeEncoded, intSubStrLen, x-intSubStrLen, subStr);
    sTempStr+=subStr;
    sTempStr+="%";
    NumberToHexString(intChar, subStr);
    sTempStr+=subStr;
    intSubStrLen=x+1;
  endif
next
GetSubstring(sToBeEncoded, intSubStrLen, x-intSubStrLen, subStr);  //get the string and append
sEncodedString=sTempStr+subStr;

example of where to use this would be if you needed to write string data to a database via a php script. i might have a string as follows: ‘here is my piece of code’. the result from running through the script would be ‘here%20is%20my%20piece%20of%20code’. url safe. only catch is that you don’t exceed the string width of the sEncodedString

hope this helps someone out there.

Nick

FYI - those of you doing string manipulation may find the new-in-9.1 command [B]Trim String [/B]useful. It’ll trim leading spaces, trailing spaces, or both.

-OptoMary