Difference between revisions of "Textutils.urlEncode"

From ComputerCraft Wiki
Jump to: navigation, search
m (added outputs)
m (Fixed)
Line 2: Line 2:
 
{{Function
 
{{Function
 
|name=textutils.urlEncode
 
|name=textutils.urlEncode
|args= [[String (type)|string]]
+
|args={{type|string}} text
|returns=A string safe for use on the internet
+
|returns={{type|string}} safe for use on the internet
 
|api=textutils
 
|api=textutils
 
|addon=ComputerCraft
 
|addon=ComputerCraft
Line 10: Line 10:
 
{{Example
 
{{Example
 
|desc=Create a safe string from another string full of invalid characters. '''This is incorrect usage!'''
 
|desc=Create a safe string from another string full of invalid characters. '''This is incorrect usage!'''
|code=<pre>sUnsafe = "http://example.com/?view=€ @!! 3-g_dvv[gf"
+
|code= local unsafeString = "http<nowiki/>://example.com/?view=€ @!! 3-g_dvv[gf"
sSafe = textutils.urlEncode(sUnsafe)
+
print(sSafe)</pre>
+
[[print]]('''textutils.urlEncode(unsafeString)''')
 
|output=http%3A%2F%2Fexample%2Ecom%2F%3Fview%3D?0%9AŒ87´A8+%40%21%21+3%2Dg%5Fdvv%5Bgf
 
|output=http%3A%2F%2Fexample%2Ecom%2F%3Fview%3D?0%9AŒ87´A8+%40%21%21+3%2Dg%5Fdvv%5Bgf
 
}}
 
}}
 
{{Example
 
{{Example
 
|desc=Create a safe URL from a base URL and a string to be inserted.
 
|desc=Create a safe URL from a base URL and a string to be inserted.
|code=<pre>sUnsafe = "€ @!! 3-g_dvv[gf"
+
|code= local unsafeString = "€ @!! 3-g_dvv[gf"
sBaseURL = "http://example.com/?view="
+
local baseUrl = "http<nowiki/>://example.com/?view="
sSafe = textutils.urlEncode(sUnsafe)
+
local safeString = '''textutils.urlEncode'''(unsafeString)
sURL = sBaseURL .. sSafe
+
print(sURL)</pre>
+
[[print]](baseUrl .. safeString)
|output=http://example.com/?view=Œ84+%40%21%21+3%2Dg%5Fdvv%5Bgf
+
|output=http<nowiki/>://example.com/?view=Œ84+%40%21%21+3%2Dg%5Fdvv%5Bgf
 
}}
 
}}
 
}}
 
}}
  
 
[[Category:API_Functions]]
 
[[Category:API_Functions]]

Revision as of 18:29, 10 April 2014


Grid Redstone.png  Function textutils.urlEncode
Replaces certain characters in a string to make it safe for use on the internet. Note that it is a string function and will even replace characters that are allowed in URLs. Use this only for text to be inserted in an URL, not the URL itself.
Syntax textutils.urlEncode(string text)
Returns string safe for use on the internet
Part of ComputerCraft
API textutils

Examples

Grid paper.png  Example
Create a safe string from another string full of invalid characters. This is incorrect usage!
Code
local unsafeString = "http://example.com/?view=€ @!! 3-g_dvv[gf"

print(textutils.urlEncode(unsafeString))
Output http%3A%2F%2Fexample%2Ecom%2F%3Fview%3D?0%9AŒ87´A8+%40%21%21+3%2Dg%5Fdvv%5Bgf



Grid paper.png  Example
Create a safe URL from a base URL and a string to be inserted.
Code
local unsafeString = "€ @!! 3-g_dvv[gf"
local baseUrl = "http://example.com/?view="
local safeString = textutils.urlEncode(unsafeString)

print(baseUrl .. safeString)
Output http://example.com/?view=Œ84+%40%21%21+3%2Dg%5Fdvv%5Bgf