 |
 |
PHP file extension check
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2000
Location: Tempe, AZ
Status:
Offline
|
|
I am listing a directory and saving all the contents into a text file. Then I am taking every row of this list into an array. I am looking for a way to check if the end if the array element end with .jpg or .gif. What PHP function can do this for me? Is there a way to check an array element's last 3 characters?
Thank you,
t
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Location: Washington, DC 20009
Status:
Offline
|
|
I'm not sure about in PHP, but normally I'd use a regex to match the last three characters that I was looking for.
Something like this in Perl:
print "This is a jpg file" if m/\.jpg$/i;
I'm not sure if the same regex syntax applies for PHP though.
|
|
Just my $.02 :-)
Ti Powerbook 1Ghz w/ Superdrive ......and lovin' it! :)
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Jun 2002
Location: New York
Status:
Offline
|
|
if(ereg("\.gif$", "$filename") or ereg("\.jpg$", "$filename"){
/*Write to file here*/
}
Be sure to remove the newline character first by trim()ing the string.
The ereg function has the following syntax: ereg(regex, string). Put both the regular expression and the string in which to search in quotes. There is more info on this at http://www.php.net/ereg .
Peter
|
|
|
| |
|
|
|
 |
|
 |
|
Moderator Emeritus 
Join Date: Mar 2001
Location: Austin, MN, USA
Status:
Offline
|
|
or:
if (substr($filename, -3) == 'jpg') $type = 'JPEG';
To me, it seems better than regular expressions. You could also do a -4 and check for '.jpg' just to be sure. As stated above, you'll have to make sure it's trim()'d so the newline doesn't count.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2000
Location: Tempe, AZ
Status:
Offline
|
|
Originally posted by Xeo:
or:
if (substr($filename, -3) == 'jpg') $type = 'JPEG';
To me, it seems better than regular expressions. You could also do a -4 and check for '.jpg' just to be sure. As stated above, you'll have to make sure it's trim()'d so the newline doesn't count.
Thank you for all of your inputs. I will try them out all.
t
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2000
Location: Tempe, AZ
Status:
Offline
|
|
For some reason the ereg function did not work, it kept giving me php errors, the substr function worked very well.
Thank you for both of you again for the response,
t
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|