Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Closed Thread
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-11-2005, 06:42 PM   PM User | #1
Velox Letum
Senior Coder

 
Join Date: Apr 2005
Location: Colorado, United States
Posts: 1,208
Thanks: 0
Thanked 0 Times in 0 Posts
Velox Letum is an unknown quantity at this point
Filename & File extension from Path

This script is for use in isolating the extension and filename from a path or URL.

PHP Code:
<?php
// Velox Letum (2005)
// elementation@gmail.com
 
$file "include/435/file2.11032424.php"// File path
 
echo substr($filestrrpos($file"/") + 1) . "<br />"// Filename, output: file2.11032424.php
echo substr($filestrrpos($file".") + 1); // File extension, output: php
?>
__________________
"$question = ( to() ) ? be() : ~be();"

Last edited by Velox Letum; 11-12-2005 at 09:35 PM..
Velox Letum is offline  
Old 12-13-2005, 07:20 PM   PM User | #2
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Quote:
Originally Posted by Velox Letum
This script is for use in isolating the extension and filename from a path or URL.

PHP Code:
<?php
// Velox Letum (2005)
// elementation@gmail.com
 
$file "include/435/file2.11032424.php"// File path
 
echo substr($filestrrpos($file"/") + 1) . "<br />"// Filename, output: file2.11032424.php
echo substr($filestrrpos($file".") + 1); // File extension, output: php
?>
Been using this, though in another form. Thought I would post it here.

It gets annoying to write out the substr() function everywhere so I made this just to make things simpler. It also allows me to use it alot more flexible.

pFV() - path File View
PHP Code:
<?php

function pFV ($url$method) {
// Velox Letum (2005) Modifications by Element
// elementation@gmail.com | firegraves@gmail.com

if(isset($url) && isset($method)) {
  if (
$method == 0) {
    
$view substr($filestrrpos($file"/") + 1); 
  } elseif(
$method == 1) {
    
$view substr($filestrrpos($file".") + 1); 
  } else {
    
$view "pFV has encounted an error evaluating your settings.";
  }
} else {
  
$view "The URL or method was left blank.";
}

return 
$view;

}

$file "include/435/file2.11032424.php"// File path

echo pFV($file0)."<br />"// Example filename, output: file2.11032424.php
echo pFV($file1); // Example file extension, output: php

if(strtolower(pFV($file 1)) == "php") { // Way of checking extension.
  
include $file;
}

?>

Last edited by Element; 12-14-2005 at 01:02 AM..
Element is offline  
Old 12-13-2005, 07:27 PM   PM User | #3
ralph l mayo
Regular Coder

 
ralph l mayo's Avatar
 
Join Date: Nov 2005
Posts: 951
Thanks: 1
Thanked 31 Times in 29 Posts
ralph l mayo is on a distinguished road
PHP Code:
<?php
$file 
'include/435/file2.11032424.php';
$parts pathinfo($file);
print_r($parts);

/* Array
(
    [dirname] => include/435
    [basename] => file2.11032424.php
    [extension] => php
) */
?>
ralph l mayo is offline  
Old 12-14-2005, 12:46 AM   PM User | #4
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Yes, that works, but that returns values that may not be used with the other method. pFV() and the regular substr() are aimed at the file. This function and method is obviously for the file, not the path.
Element is offline  
Old 12-14-2005, 12:05 PM   PM User | #5
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
Quote:
Originally Posted by Element
Yes, that works, but that returns values that may not be used with the other method. pFV() and the regular substr() are aimed at the file. This function and method is obviously for the file, not the path.
What do you mean? pathinfo() is usually the best option for this. You can also specify a single bit of information to return... Take a look at http://www.php.net/pathinfo.
missing-score is offline  
Old 12-14-2005, 04:45 PM   PM User | #6
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Quote:
Originally Posted by missing-score
What do you mean? pathinfo() is usually the best option for this. You can also specify a single bit of information to return... Take a look at http://www.php.net/pathinfo.
I just mean that this function is entitled specifically for the file of the path. Sure pathinfo() can do it, but if you thought about it that way there are alot of duplicated functions that can do the same things that others can. Though these duplicates also have they're own purpose. Just like now pFV() is specifically for the file, ignoring anything before the last forward slash. And where as if I used pathinfo() I would be writing more whenever I want to use it. Now why would I want to do that? Pshaw. Lol.

Sorry if I'm weird but in our school are teacher thought it was cheating to rely on PHP functions when you can create them specifically for your scripts needs. It makes it alot more custom and shows more of the users creativety.

Okay, well its 8:43 AM, time for a couple more posts and maybe going back to bed before college. >.<
Element is offline  
Old 12-14-2005, 04:55 PM   PM User | #7
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
Quote:
Originally Posted by Element
Sorry if I'm weird but in our school are teacher thought it was cheating to rely on PHP functions when you can create them specifically for your scripts needs. It makes it alot more custom and shows more of the users creativety.
I think you should possibly start ignoring your teacher. Seriously, PHP functions are there for just that reason.. to be used. Most are highly optimized in the core PHP interpreter. I'm quite surprised your teacher would tell you to replicate core PHP functions. It is by no means cheating to use the functionality PHP provides.

Also, my point is there is nothing special about your function that separates it from pathinfo, except that it doesn't do quite as much and is going to be slower than the built in function.
missing-score is offline  
Old 12-14-2005, 05:01 PM   PM User | #8
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Quote:
Originally Posted by missing-score
I think you should possibly start ignoring your teacher. Seriously, PHP functions are there for just that reason.. to be used. Most are highly optimized in the core PHP interpreter. I'm quite surprised your teacher would tell you to replicate core PHP functions. It is by no means cheating to use the functionality PHP provides.

Also, my point is there is nothing special about your function that separates it from pathinfo, except that it doesn't do quite as much and is going to be slower than the built in function.
Actually, its faster then pathinfo(), pathinfo() executes in 2.1 seconds on a blank page and pFV() executes in 1.7 seconds.

As far as my teacher, we use plenty of PHP functions, we just choose to take a piece of the cake, rather then the whole thing. Once again just because the function does the same thing doesn't mean its ideal for some situations. Just like with MySQL there are alot of methods of outputting MySQL results, some come out the same though may be longer then another. In a nut shell, there is no reason to argue or even mention pathinfo() because a duplicated function or something that does the same thing isn't going to end your world.

Everyone no matter what you think say or do will choose to code how they like. And considering I like my teacher and he is a program specialist I like his teachings. Honestly would you spend the time to make functions that do just what you want them to do? Obviously not. Well he does, and he gets paid up the *** for it.

If the college lab wasn't under intranet I could show you the entire Student CMS manager, it has a little file that lets you view the PHP sources of the pages.

Basically in conclusion. If you didn't like playing with functions and such, like we do in class (And we don't make functions like this one) then there would be no reason to upgrade PHP becuase it is people that make functions that open up new functions to add to PHP.

Last edited by Element; 12-14-2005 at 05:12 PM..
Element is offline  
Old 12-14-2005, 05:13 PM   PM User | #9
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
I think you dont fully understand how to use the pathinfo function, I wrote a test script to test its speed with random filenames. I used a loop of 10,000 for each:

Code:
Time taken on pFV method: 0.417667 seconds.
Time taken on pathinfo method: 0.295091 seconds.
Was my average outcome.

Here is the script I used to generate the results:

PHP Code:
<?php

function pFV ($url$method) {
// Velox Letum (2005) Modifications by Element
// elementation@gmail.com | firegraves@gmail.com

if(isset($url) && isset($method)) {
  if (
$method == 0) {
    
$view substr($urlstrrpos($url"/") + 1); 
  } elseif(
$method == 1) {
    
$view substr($urlstrrpos($url".") + 1); 
  } else {
    
$view "pFV has encounted an error evaluating your settings.";
  }
} else {
  
$view "The URL or method was left blank.";
}

return 
$view;

}

$pFV_start microtime(1);
for( 
$i 0$i 10000$i++ ){

    
$randomname explode"|"chunk_splitmd5uniqid() ), 8"|" ) );
    
$randomname "{$randomname[0]}/{$randomname[1]}/{$randomname[2]}.{$randomname[3]}";

    
$filename pFV$randomname);
    
$extension pFV$randomname);
    
}
$pFV_total = ( microtime(1) - $pFV_start );
echo 
'Time taken on pFV method: <strong>' round$pFV_total) . '</strong> seconds.';

$pit_start microtime(1);
for( 
$i 0$i 10000$i++ ){

    
$randomname explode"|"chunk_splitmd5uniqid() ), 8"|" ) );
    
$randomname "{$randomname[0]}/{$randomname[1]}/{$randomname[2]}.{$randomname[3]}";

    
$filename pathinfo$randomnamePATHINFO_BASENAME );
    
$extension pathinfo$randomnamePATHINFO_EXTENSION );
    
}
$pit_total = ( microtime(1) - $pit_start );
echo 
'<br />Time taken on pathinfo method: <strong>' round$pit_total) . '</strong> seconds.';

?>
My point is, the Pathinfo function is capable of doing more, and is faster than a custom coded equivilent, and is built into PHP and therefore uses less code.
missing-score is offline  
Old 12-14-2005, 05:19 PM   PM User | #10
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Quote:
Originally Posted by missing-score
I think you dont fully understand how to use the pathinfo function, I wrote a test script to test its speed with random filenames. I used a loop of 10,000 for each:

Code:
Time taken on pFV method: 0.417667 seconds.
Time taken on pathinfo method: 0.295091 seconds.
Was my average outcome.

Here is the script I used to generate the results:

PHP Code:
<?php

function pFV ($url$method) {
// Velox Letum (2005) Modifications by Element
// elementation@gmail.com | firegraves@gmail.com

if(isset($url) && isset($method)) {
  if (
$method == 0) {
    
$view substr($urlstrrpos($url"/") + 1); 
  } elseif(
$method == 1) {
    
$view substr($urlstrrpos($url".") + 1); 
  } else {
    
$view "pFV has encounted an error evaluating your settings.";
  }
} else {
  
$view "The URL or method was left blank.";
}

return 
$view;

}

$pFV_start microtime(1);
for( 
$i 0$i 10000$i++ ){

    
$randomname explode"|"chunk_splitmd5uniqid() ), 8"|" ) );
    
$randomname "{$randomname[0]}/{$randomname[1]}/{$randomname[2]}.{$randomname[3]}";

    
$filename pFV$randomname);
    
$extension pFV$randomname);
    
}
$pFV_total = ( microtime(1) - $pFV_start );
echo 
'Time taken on pFV method: <strong>' round$pFV_total) . '</strong> seconds.';

$pit_start microtime(1);
for( 
$i 0$i 10000$i++ ){

    
$randomname explode"|"chunk_splitmd5uniqid() ), 8"|" ) );
    
$randomname "{$randomname[0]}/{$randomname[1]}/{$randomname[2]}.{$randomname[3]}";

    
$filename pathinfo$randomnamePATHINFO_BASENAME );
    
$extension pathinfo$randomnamePATHINFO_EXTENSION );
    
}
$pit_total = ( microtime(1) - $pit_start );
echo 
'<br />Time taken on pathinfo method: <strong>' round$pit_total) . '</strong> seconds.';

?>
My point is, the Pathinfo function is capable of doing more, and is faster than a custom coded equivilent, and is built into PHP and therefore uses less code.
I don't understand, I used that same code and I get random results, sometimes pathinfo() is faster, and sometime pFV(). Though I'm on dialup as well. I'll try it on my computer.
Element is offline  
Old 12-14-2005, 05:24 PM   PM User | #11
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
My results come from 100 repeated tests.. I dont see any difference (except lack of features) between your function and the built in function, although you say there is.

Quote:
Originally Posted by Element
Basically in conclusion. If you didn't like playing with functions and such, like we do in class (And we don't make functions like this one) then there would be no reason to upgrade PHP becuase it is people that make functions that open up new functions to add to PHP.
I agree with you fully, but why replicate built in functions, and make them slower at the same time... I dont doubt that your teacher is a qualified professional. My maths teacher was qualified professional, but she was a terrible teacher, and many of her methods didnt make sense.

The only time functions like that are good is for learning about how to use other functions (like substr() and strrpos()). In fact... why did you even use the substr function? Shouldnt you have used a for loop and accessed each variable character with $var{character} ?

Quote:
Originally Posted by Element
Everyone no matter what you think say or do will choose to code how they like.
Again, I agree... Everyone has their own little quirks of programming, preferred methods, and adaptations on design patterns. That doesn't mean there isn't a bad and good way to do it.

Say you need to select 10 rows from a database of 10,000 rows... Do you limit the query to only select 10, or do you select all 10,000 and then get PHP to filter out just 10. Obviously, the good way to do it is to limit the query, but I have seen it done the other way. Regardless of personal preference, selecting all 10,000 rows when you only need ten is the bad way to do it.
missing-score is offline  
Old 12-14-2005, 05:29 PM   PM User | #12
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
I'm thankfull that I learned PHP by myself.

I just might add that PHP also has basename(), dirname() functions and the __FILE__ magic constant.

You can also let your scripts be nice and realtive and let the realpath() function add the full path.

On a final note: It is always better to use PHP functions as they will be updated (optimized/whatever) without you having to do anything.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline  
Old 12-14-2005, 05:38 PM   PM User | #13
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Oh yeah, see only reason I used the substr() method was because the script a used for it. (An upload script) My friend asked how I could get the extension with substr() instead of pathinfo() or exploding it. So, I used this and also gave him the function because he kept writing the substrs wrong even though they're on the page to look at.

Honestly he doesn't know what he is doing with most of my friend, so I have often found myself moping(sp) about easy ways.

For example. I had a class that I edited and modified to make signatures based off of all the backgrounds i've made and gif images (Which are people, like Britney Spears, Korn, etc.) Well when I gave it to a friend I spent a week trying to get it too work for them. By the time I was done I had just realized I could have made an easy 130 dollars. Thats why I stick with easy code most the time because I quite litterally give my code away to anyone that asks, I don't believe in selling scripts because I would never buy a script.

I paid for the invision lisense when 1.3 final came out and was very dissapointed at how horrible the syntax was. It made everything hard to edit because it was not human readable (Well it was but not like what your teachers teach you, to make the code noticable as you scan through the file looking for something).
Element is offline  
Old 12-14-2005, 05:57 PM   PM User | #14
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
The easy way is not always the best way.

I don't know what the IPB Developers have to offer in coding style but writing code in a hard to read but short way is the easy, but not the best way as you see (phpBB has very good coding guidelines).
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline  
Old 12-14-2005, 06:04 PM   PM User | #15
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Quote:
Originally Posted by marek_mar
The easy way is not always the best way.

I don't know what the IPB Developers have to offer in coding style but writing code in a hard to read but short way is the easy, but not the best way as you see (phpBB has very good coding guidelines).
It has nothing to do with style or guidelines, its just common sense which any book or teacher teaches you from day one, always use pseudo code to explain large more complex code so a use doesn't have to spend a minutes reading out the code to imagine what it will do.

And as for the style, I thinky ou mean syntax. Developing easy to read through code like:

PHP Code:
if($var == true) {

  if(
$var == true) {

    if(
$var == true) {

    } else {

    }

  } else {

  }

} else {


Which obviously looks so much better then solid blocks of code like:

PHP Code:
if($var == true) {
if(
$var == true) {
if(
$var == true) {
} else {
}
} else {
}
} else {

See when reading alot of code like that, imagine more code in it, like 100 lines in each, it gets frustrating looking for correct closing curly brackets where as with correctly formated syntax you can easily find the curly bracket because you know what position the opening one is at. Like, two spaces in, for example.
Element is offline  
Closed Thread

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:37 PM.


Advertisement
Log in to turn off these ads.