Page 1 of 1

[SOLVED]breaking up text

Posted: Sat Jun 11, 2005 12:58 pm
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

Posted: Sat Jun 11, 2005 1:28 pm
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

Posted: Sat Jun 11, 2005 5:44 pm
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

Posted: Sat Jun 11, 2005 7:48 pm
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.