Forum Moderators: coopster

Message Too Old, No Replies

Get the beginning occurrence of a string

         

dulldull

8:33 pm on Jul 3, 2008 (gmt 0)

10+ Year Member



hello folks,

When i want to get the file extension of a string,
it can be done by using:
substr(strrchr("abc.txt", "."), 1)

If i want to get the filename (the length of the name is not fixed),
how can i do it?

Thanks!

Receptional Andy

8:38 pm on Jul 3, 2008 (gmt 0)



One way would be to split the string into two based on the occurence of the 'dot' using a function like explode [php.net]:

$string='abc.txt';

$pieces = explode('.', $string);

$filename=$pieces[0]; // abc

$file_extension=$pieces['1']; // txt

Edit: the single quotes around the 1 in ['1'] are un-necessary, but I had to include them to avoid triggering BB code

[edited by: Receptional_Andy at 8:42 pm (utc) on July 3, 2008]

dulldull

12:48 am on Jul 4, 2008 (gmt 0)

10+ Year Member



Thanks a lot Andy. It's genius!

dreamcatcher

7:00 am on Jul 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



or without the split, use strpos [php.net] and substr [php.net].

$string = 'abc.txt';
$string = substr($string,0,strpos($string,'.'));

dc

dulldull

8:38 pm on Jul 4, 2008 (gmt 0)

10+ Year Member



Thanks DC, the code is cleaner and i like it too!

Receptional Andy

8:51 pm on Jul 4, 2008 (gmt 0)



the code is cleaner

I have a fairly limited knowledge of PHP, and frankly, of coding best practices. I assumed that running one function would be preferable to running two. Any comments from PHP people?

Little_G

9:05 pm on Jul 4, 2008 (gmt 0)

10+ Year Member



Hi,

As far as I'm concerned whichever is fastest is the best.
My benchmark shows using substr is faster, slightly. I would assume in this instant that the explode approach is slower because of the construction of an array.

Andrew

Receptional Andy

9:10 pm on Jul 4, 2008 (gmt 0)



Would preg_match be preferable? I avoided regular expressions since I thought that using explode was a more 'natural' way to separate a string based on a dividing character.

henry0

9:39 pm on Jul 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From the manual, assuming that you know where that file comes from ):

$path = "/home/httpd/html/index.php";
$file = basename($path); // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"

Little_G

10:01 pm on Jul 4, 2008 (gmt 0)

10+ Year Member



Hi,

Would preg_match be preferable?
Using regular expressions is always likely to be slower with simple string extraction like this.

Also, basename still doesn't beat substr+strpos on speed!

Andrew

dulldull

10:46 pm on Jul 4, 2008 (gmt 0)

10+ Year Member



Would preg_match be preferable? I avoided regular expressions since I thought that using explode was a more 'natural' way to separate a string based on a dividing character

yes, i agree. I find it very easy and natural to understand your method and I'm using it now. As a beginnner, I've often faced some codes that I put in the php, but forgot what it means during the debug process.

eelixduppy

3:39 am on Jul 7, 2008 (gmt 0)



>> As far as I'm concerned whichever is fastest is the best.

Not the best way to look at things IMO. Sure you want things to be fast, but for most simple string manipulations and stuff, such as this, speed really isn't too much of an issue. Readability in a case like this would be my number one concern. The cleaner and easier the code is to read and understand, the better the option is. In this situation all the solutions proposed aren't messy, so this doesn't really apply here, but let's just say that some things can get messy quick ;)

dreamcatcher

7:34 am on Jul 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are many ways to use the PHP functions, so its each to his own. Its always nice to see alternative ways of doing things. I`m with eelixduppy, I tend to favour the cleaner option, rather than the fastest. But it doesn`t matter how you lay out your code as long as it works for you. Everyone has different ways of coding. Just do what you feel comfortable with.

dc