Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > PHP file extension check

PHP file extension check
Thread Tools
Mac Elite
Join Date: Dec 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Dec 4, 2002, 12:27 PM
 
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
Reply With Quote
Dec 4, 2002, 01:51 PM
 
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
Reply With Quote
Dec 6, 2002, 01:03 PM
 
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
     
Xeo
Moderator Emeritus
Join Date: Mar 2001
Location: Austin, MN, USA
Status: Offline
Reply With Quote
Dec 8, 2002, 03:59 AM
 
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.
     
t_hah  (op)
Mac Elite
Join Date: Dec 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Dec 8, 2002, 08:11 AM
 
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
     
t_hah  (op)
Mac Elite
Join Date: Dec 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Dec 11, 2002, 12:17 PM
 
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
     
   
Thread Tools
Forum Links
Forum 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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:26 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2