Forum Moderators: coopster

Message Too Old, No Replies

Get a "file.php" separate from the extension?

         

nimonogi

10:14 pm on Apr 2, 2006 (gmt 0)

10+ Year Member



Hello,

Is it available in PHP to get a "file.php" separate from the extension? ex. "file"

Thanks

TomAnthony

1:56 am on Apr 3, 2006 (gmt 0)

10+ Year Member




$filename = 'file.php';
echo substr($filename, 0, strrpos($filename, '.'));

That should do what you need. :)

nimonogi

5:26 am on Apr 3, 2006 (gmt 0)

10+ Year Member



Thanks TomAnthony,

The code is working fine; but if my extension have 2 dots? ex. file.php.1
Can i get the "file" now?

$filename = $_SERVER['PHP_SELF'];
$full = 'http://'.$_SERVER['HTTP_HOST'].substr($filename, 0, strrpos($filename, '.'));
echo $full;

Thanks a lot!

scriptmasterdel

9:29 am on Apr 3, 2006 (gmt 0)

10+ Year Member



You could always do it like this ....

<?
$file = "file.php";
$filename = substr($file,0,-4);
echo $filename;
?>

this basically just removes the last four characters, so you will have no problems if the filename has more then one dot ;)

Del

TomAnthony

12:04 pm on Apr 3, 2006 (gmt 0)

10+ Year Member



scriptmasterdel - I believe the desired response is to get 'file' still, removing the last 4 characters from 'file.php.1' gives you 'file.p'.

nimonogi - Try this:


$filename = 'file.php';
echo substr($filename, 0, strpos($filename, '.'));

Before we were using strrpos() which searched for the last dot, and stripped everything after it.

Now we are using strpos() which searches for the first dot and strips everything after it.

nimonogi

2:26 pm on Apr 3, 2006 (gmt 0)

10+ Year Member



TomAnthony, this is exactly what i was looking for.

Thank you.

nimonogi

12:43 pm on Apr 5, 2006 (gmt 0)

10+ Year Member



Hello again,

I face a problem with IE and the code noted below.
FireFox works just great but IE screw them up when using paths. ex. Instead of domain.com/path/file.php i get domain.com/path/ (domain.com/path/index.php)

'http://'.$_SERVER['HTTP_HOST'].substr($_SERVER['PHP_SELF'], 0, strpos($_SERVER['PHP_SELF'], '.'));

Does anybody know how to fix this?

Thanks.

nimonogi

12:50 pm on Apr 5, 2006 (gmt 0)

10+ Year Member



Actually these problem is general in IE not just in paths.

When trying to load domain.com/file.php i getting domain.com/ (domain.com/index.php)

TomAnthony

3:29 pm on Apr 5, 2006 (gmt 0)

10+ Year Member



Try this:


'http://'.$_SERVER['HTTP_HOST']. basename(substr($_SERVER['PHP_SELF'], 0, strpos($_SERVER['PHP_SELF'], '.')));

This should work fine too, fi your files are always '.php' and not anything else:


'http://'.$_SERVER['HTTP_HOST']. basename($_SERVER['PHP_SELF'], '.php');

Look up basename() which will explain what is going on.

Does that do what you need?

nimonogi

4:57 pm on Apr 5, 2006 (gmt 0)

10+ Year Member



I am ashamed tell this but the code was ok, i was made a mistake and not include the "echo $abc" just "$abc". FireFox was recognized it but IE not.

Thanks a lot TomAntony