[SOLVED]breaking up text

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

[SOLVED]breaking up text

Post by thallish »

hi all

i want to get the name of the file that i'm currently working with. i have an idea to get $_SERVER['PHP_SELF'] and then break up the text string to do that. If i get the string /kompetencesystem/default.php i need to get the default.php part from it.

Now my question is:

Is this the simplest way to do it? and if it is how is this achieved?

any hints will be useful thx

regards

thallish
Last edited by thallish on Sun Jun 12, 2005 7:21 am, edited 1 time in total.
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post by thallish »

OK i solved it already :wink:

here is what i did

Code: Select all

$site = $_SERVER['PHP_SELF'];

$array = explode('/', $site);

$array = array_reverse($array);

$site = $array[0];
Now does this look reasonable?

thallish
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

It sure does, but obviously, me being me will have to jump in and do a regex :P

Code: Select all

$this_file = $_SERVER['PHP_SELF']; # /app1/classes/classname.class.php

$this_file = preg_replace('#([^/]+)$#', '$1', $this_file); # classname.class.php
Your way does the job, my way is just typically my regex way :P
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

d11, regex may be extremely useful and all, but a lot of the times it can really slow down page execution time. I thought this would be a perfect example to run a quick benchmark.

I ran both sets of codes, yours and thallish, 10 000 times in a loop to get an semi-accurate average.

Results:

Thallish: Execution took on average (0.015681743621826) Seconds
d11wtq: Execution took on average (0.030075311660767) Seconds

Conclusion: Regex may not always be the best solution, althought I salute your cunningness.
Post Reply