Jump to content

Qick way to find last \ in a URL.


tommytx
 Share

Recommended Posts

Does anybody know of short quick simple command to find the last \ in the url... I know i can splitstring orcount them and figure it out..

but surely as nice as autoit is.. there must be something simple... Please.
What i need is to grab the filename at the end of each line.. like colors.php
So i could split at the \ and grab the last array item... but if anyone has something simpler
I would appreciate it.. Thanks
 

shorty\wp-content\plugins\IDXSEO\css\colors.php -> 
shorty\wp-content\plugins\IDXSEO\Geocoding\Geocron.php -> 
shorty\wp-content\plugins\IDXSEO\include\CRM.php -> 
shorty\wp-content\plugins\IDXSEO\include\FieldCategoriesFrontend.php

 

Link to comment
Share on other sites

OOPS!  Sorry guys this was way simpler than i though in my mind.. I was not thinking about even though the array might have 20 items in some cases they only thing I need todo is simply get the last split... so I will use this.. but would still like to know if anything simpler than this is available.. Thanks..

$dog = "shorty\wp-content\plugins\IDXSEO\css\colors.php"
$cat = StringSplit($dog, "\")
ConsoleWrite($cat[$cat[0]] & @CRLF)
Prints - colors.php

 

Link to comment
Share on other sites

Here all together :

#include <File.au3>

Local $dog = "shorty\wp-content\plugins\IDXSEO\css\colors.php"
Local $cat

; @tommytx :
$cat = StringSplit($dog, "\")
ConsoleWrite("1. <" & $cat[$cat[0]] & ">" & @CRLF)

; @mikell :
$cat = StringRegExpReplace($dog, "^.+\\", "")
ConsoleWrite("2. <" & $cat & ">" & @CRLF)

; with _PathSplit :
Local $sDrive = "", $sDir = "", $sFileName = "", $sExtension = ""
_PathSplit($dog, $sDrive, $sDir, $sFileName, $sExtension)
ConsoleWrite("3. <" & $sFileName & $sExtension & ">" & @CRLF)

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

is a small variation that saves 2 bytes allowed on my previous post?  :P

Local $dog = "shorty\wp-content\plugins\IDXSEO\css\colors.php"
MsgBox(0, '', StringTrimLeft($dog, StringInStr($dog, '\', 0, -1)))

p.s.

in versions that uses an array, if you want to save some bytes you can avoid using the $cat variable like this:

Local $dog = "shorty\wp-content\plugins\IDXSEO\css\colors.php"
Msgbox(0,"", StringRegExp($dog, "[^\\]+$", 1)[0])

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

52 minutes ago, Chimp said:

Local $cat = StringTrimLeft($dog, StringInStr($dog, '\', 0, -1))

Chimp's code is exactly the one I'm using for a long time.

When it comes to check the extension only, then '.' replaces '\'

Local $sExt = StringTrimLeft($dog, StringInStr($dog, '.', 0, -1))

StringTrimRight is not forgotten, for example to get the path & name of an eventual ini file matching the script

Local $sIniFile = StringTrimRight(@ScriptFullPath, 3) & "ini"

This is endless... I guess habits are hard to change sometimes :)

Link to comment
Share on other sites

16 hours ago, Musashi said:

Here all together :

Update...

The test results:

tommytx  : stmt length: 31 output:colors.php
Time for 1000: 6.5914 each: 0.0066

mikell   : stmt length: 31 output:colors.php
Time for 1000: 6.5373 each: 0.0065

pathsplit: stmt length: 32 output:colors.php
Time for 1000: 19.8721 each: 0.0199

chimp    : stmt length: 43 output:colors.php
Time for 1000: 4.4016 each: 0.0044

The code* tested:

#include <File.au3>
Global $p="shorty\wp-content\plugins\IDXSEO\css\colors.php"

RunTest()

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func tommytx($s)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

$s=StringSplit($s,"\")

Return $s[$s[0]]

EndFunc

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func mikell($s)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Return StringRegExp($s,"[^\\]+$",1)[0]

EndFunc

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func pathsplit($s)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Return _PathSplit($s,"","","",$s)[3]&$s

EndFunc

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func chimp($s)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Return StringTrimLeft($s,StringInStr($s,'\',0,-1))

EndFunc

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func RunTest()
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

ConsoleWrite("tommytx: length: 31 output:"& tommytx($p) &@CRLF)
TimeIt(tommytx,1000)

ConsoleWrite("mikell: length: 31 output:"& mikell($p) &@CRLF)
TimeIt(mikell,1000)

ConsoleWrite("pathsplit: length: 32 output:"& pathsplit($p) &@CRLF)
TimeIt(pathsplit,1000)

ConsoleWrite("chimp: length: 43 output:"& chimp($p) &@CRLF)
TimeIt(chimp,1000)

 

*code has been optimized for whitespace and identifiers - please respond with any further optimizations or objections to the current optimization.

 

Code hard, but don’t hard code...

Link to comment
Share on other sites

On 11/22/2020 at 8:23 AM, Musashi said:

Here all together :

For the sake of completeness : This was meant as "all", which were mentioned up to this point ;).

There are of course numerous ways to skin the cat, as shown hereafter.

8 hours ago, JockoDundee said:

The test results:

Nice addition to do a performance test.

Edited by Musashi
typo

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...