Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Parsing / Split File Name From a URL (%20 space)

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Corroder
Grandmaster Cheater
Reputation: 17

Joined: 10 Apr 2015
Posts: 613

PostPosted: Mon Apr 23, 2018 11:21 pm    Post subject: Parsing / Split File Name From a URL (%20 space) Reply with quote

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

_________________
Singapore Sling + Johnnie Walker + Some Peanuts....
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater
Reputation: 30

Joined: 09 Aug 2013
Posts: 716

PostPosted: Tue Apr 24, 2018 5:27 am    Post subject: Reply with quote

% 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)

_________________
https://github.com/FreeER/ has a few CE related repos
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater
Reputation: 17

Joined: 10 Apr 2015
Posts: 613

PostPosted: Tue Apr 24, 2018 9:04 am    Post subject: Reply with quote

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...

_________________
Singapore Sling + Johnnie Walker + Some Peanuts....
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites