|
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
Corroder Grandmaster Cheater Supreme Reputation: 75
Joined: 10 Apr 2015 Posts: 1667
|
Posted: Mon Apr 23, 2018 11:21 pm Post subject: Parsing / Split File Name From a URL (%20 space) |
|
|
Hi there,
Code: | url = 'http://link.planetlagu.site/save/01%20Rich%20Chigga%20Glow%20Like%20Dat.mp3'
fn = url:match( "([^/]+)$" )
print(fn)
-- result = 01%20Rich%20Chigga%20Glow%20Like%20Dat.mp3
fn = string.gsub(fn, "%20", " ")
-- result = invalid capture index %2 |
So i try to parsing mp3 file name from a mp3 url. But as shown on code block above I am facing problem with this '%20 (space character)' as invalid index. I want result as : '01 Rich Chingga Glow Like Dat.mp3'.
Any solution ?. Thank you
_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL |
|
Back to top |
|
|
FreeER Grandmaster Cheater Supreme Reputation: 53
Joined: 09 Aug 2013 Posts: 1091
|
Posted: Tue Apr 24, 2018 5:27 am Post subject: |
|
|
% is the formatting character used in lua's pattern matching eg. %d is a digit character, use %% to escape it.
20 is the hex value for space, I know there are other % encodings that are possible, if they're all the same then you could use this to handle all of them
Code: | fn = string.gsub(fn, "%%(%x%x)", function(cap) return string.char(tonumber(cap,16)) end) |
_________________
|
|
Back to top |
|
|
Corroder Grandmaster Cheater Supreme Reputation: 75
Joined: 10 Apr 2015 Posts: 1667
|
Posted: Tue Apr 24, 2018 9:04 am Post subject: |
|
|
Thanks @freeER
Your code :
Code: | url = 'http://link.planetlagu.site/save/01%20Rich%20Chigga%20Glow%20Like%20Dat.mp3'
fn = url:match( "([^/]+)$" )
fn = string.gsub(fn, "%%(%x%x)", function(cap) return string.char(tonumber(cap,16)) end)
-- result = 01 Rich Chingga Glow Like Dat.mp3 |
Anyway, I found a general function from garrysmod/garrysmod/lua/includes/extensions/string.lua with has function
to find and replace any character in a URL.
Code: | function explode(separator, str, withpattern)
if ( withpattern == nil ) then withpattern = false end
local ret = {}
local current_pos = 1
for i = 1, string.len( str ) do
local start_pos, end_pos = string.find( str, separator, current_pos, not withpattern )
if ( not start_pos ) then break end
ret[ i ] = string.sub( str, current_pos, start_pos - 1 )
current_pos = end_pos + 1
end
ret[ #ret + 1 ] = string.sub( str, current_pos )
return ret
end
function replace( str, tofind, toreplace )
local tbl = explode( tofind, str )
if ( tbl[ 1 ] ) then return table.concat( tbl, toreplace ) end
return str
end
url = 'http://link.planetlagu.site/save/01%20Rich%20Chigga%20Glow%20Like%20Dat.mp3'
fn = url:match( "([^/]+)$" )
print(replace(fn, "%20", " ")) --- result = 01 Rich Chigga Glow Like Dat.mp3
fn1 = replace(url, "%20", " ")
fn1 = replace(fn1, "//", " ")
print(replace(fn1, "/", " ")) --- result = http: link.planetlagu.site save 01 Rich Chigga Glow Like Dat.mp3
-- and so on...
|
_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|