Jump to content

cut last part of a string

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
my brain is currently soaked and i cant think.

i want to use $_SERVER["SCRIPT_FILENAME"]
to find out the full path the script lies in. but this includes the filename, and i don't want that

how to cut the last "/" (or "\" in windows) and the filename
by easieast way? general for both win & *nix ofcourse

#2
mikelbring

mikelbring

    Programmer

  • Members
  • PipPipPipPip
  • 118 posts
Use dirname(__FILE__) instead. It will show the full root of the file without the filename.

Realize the Web Web services and design.


#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
I would do what mikelbring recommended. However, one thing many people do not know about is the DIRECTORY_SEPARATOR constant - it will be \ on windows and / on Linux. So if for some reason, you wanted to accomplish this using string manipulations, you could do:
$string = implode(DIRECTORY_SEPARATOR, 
                  explode(DIRECTORY_SEPARATOR, 
                          $_SERVER['SCRIPT_FILENAME'], -1));


#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
You could also use substr and strrpos:

$path = substr($_SERVER['SCRIPT_FILENAME'], 0, strrpos(DIRECTORY_SEPARATOR));


#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
that was what John wrote, word by word, do you have something to add that's not someone elses words?

#6
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Seems like spam. Deleting/banning.

#7
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Don't you dare call orjan a spammer. :P
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#8
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
haha ther ewas some jerk who added some annoying thing that Jordan did remove. well, he could have removed my comment to it as well for the flow of the tread...

#9
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
I guessed! lol
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums