Forum Moderators: coopster
$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]
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?
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.
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 ;)
dc