0

I have been trying to fix this for the past few hours and I cannot seem to get anything working. My question is straightforward, I need to convert some strings that are being inputted to a URL so that it correctly sends the spaces and special characters to the receiving server.

I have 3 strings: setFirstLast.text, emailAddress.text, and visitingTest.text. I need to convert them into something that prints like: John%20Smith instead of John Smith.

Abishek
  • 11,191
  • 19
  • 72
  • 111
user3282173
  • 103
  • 1
  • 10

2 Answers2

0

Here, $url1 is your original url.

$url1 = "http://X.X.X.X/print/postToDB.php?namestamp=John Smith&emailstamp=john@example.com&visitingstamp=Bob Clark";

You could do:

$url2 = urlencode($url1);
//prints: http%3A%2F%2FX.X.X.X%2Fprint%2FpostToDB.php%3Fnamestamp%3DJohn%20Smith%26emailstamp%3Djohn%40example.com%26visitingstamp%3DBob%20Clark%0A

or

$url3 = str_replace(' ','%20', $url1);
//prints: http://X.X.X.X/print/postToDB.php?namestamp=John%20Smith&emailstamp=john@example.com&visitingstamp=Bob%20Clark

Does this help?

lingu
  • 36
  • 6
  • Mike, thanks for the quick response. I should have been more clear in my question. I'm trying to encode the string as it leaves the iOS device, not after it arrives at the server. My apologies. I will revise the question. – user3282173 Feb 07 '14 at 03:32
  • If this is in terms of objective-c, you may want to look at http://stackoverflow.com/questions/8086584/objective-c-url-encoding. – Abishek Feb 07 '14 at 04:12
  • YES! That worked. I can't believe I didn't come across that article. Much appreciate Abishek! Thank you so much. – user3282173 Feb 07 '14 at 04:33
0

You need to use

[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

to get the desired result. string is the object containing your original text and NSUTF8StringEncoding is one of many encoding options available.

Akshat Singhal
  • 1,801
  • 19
  • 20