Forum Moderators: coopster

Message Too Old, No Replies

ereg problem

trying to find a variable in URL and return it

         

TheSpeshulK

6:34 pm on Mar 20, 2008 (gmt 0)

10+ Year Member



Hello Webmaster World.
So here's the problem: I'm trying to use ereg() to look at my current URL and echo back (in $tags) a substring of the URL. Right now the URL's are setup 'www.domain.com/var1/subvar1.html'.
for my cod I'm trying to use:

ereg("/.*?/?([A-Za-z_]*)\.php/", $_SERVER['PHP_SELF'], $tags); echo $tags[1]

I'm pretty sure the problem is in the regular expression string but i just can't seem to find what the problem may be. 'PHP_SELF' is relative to the server so I don't have to worry about looking past that in my regular expression string but sometimes there is no 'subvar' so I would want to just return 'var1.html' Any help would be great and thanks in advance.

TheSpeshulK

7:55 pm on Mar 20, 2008 (gmt 0)

10+ Year Member



Found a work around:

$strings = explode("/", $_SERVER['PHP_SELF']);
$i = count($strings);
$var = explode(".", $strings[$i-1]);
$place = ucwords(str_replace("_", " ", $var[0]));

Now i just echo back $place wherever I want the var to appear on the page.

coopster

9:11 pm on Mar 20, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Are you just trying to get the filename? pathinfo [php.net] may be easier. If you are running PHP >= 5.2.0 you can use the
PATHINFO_FILENAME
constant otherwise you'll have to do the extra work as you have done here to pull it out of the
PATHINFO_BASENAME
constant (or use basename [php.net]).
print pathinfo($_SERVER['REQUEST_URI'], PATHINFO_BASENAME); 
// Or, with PHP >= 5.2.0
print pathinfo($_SERVER['REQUEST_URI'], PATHINFO_FILENAME);

Of course, you still have to replace your underscores, etc. as well.