Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Find string as value from array?
Old 07-02-2010, 01:08 PM Find string as value from array?
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Hey guys,

I was wondering if it is possible to find a string in an array. Such as this:

PHP Code:
$imgFileExt = array(".png",".jpg",".jpeg",".svg",".ico",".gif");//array full of image file extensions
$imgCheck array_values($imgFileExt);//get the array's values
if (preg_match("/$imgCheck/i"$fileTR)) { //checks if there's a match with the array values and the file to read
          
echo "This file is an image";//if there is, tell the user it's an image

Basically I want to find if the file ($fileTR) contains '.png','gif','jpeg'.......etc. But it's not working

What am I doing wrong?

Thanks

-PG
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 07-02-2010 at 01:10 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-02-2010, 01:15 PM Re: Find string as value from array?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
in_array
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-02-2010, 01:27 PM Re: Find string as value from array?
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Yeah I tried that... It didn't work...

I'll try again, maybe I did something funky.

Ah, it turns out I did do something funky. I don't know what, so I just rewrote the thing from scratch and now it works Thanks!
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 07-02-2010 at 01:36 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 07-02-2010, 01:28 PM Re: Find string as value from array?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
The function array_values returns all of the values (not keys) in an array. Calling it on a non-associative array (as you are doing here) won't have any affect; the input is the same as the output.

Since $imgCheck is an array the line:
PHP Code:
if (preg_match("/$imgCheck/i"$fileTR)) 
will not work as intended.

The way I would do this would be to get the extension from the file name, then check if it is in the array:
PHP Code:
$imgFileExt = array(".png",".jpg",".jpeg",".svg",".ico",".gif");
$expr '/\.([^\.]+)$/';

$match;
preg_match($expr$fileTR$match);
if(
in_array($match[0], $imgFileExt))
{
    echo 
'image';

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 07-02-2010, 01:37 PM Re: Find string as value from array?
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Yeah, that's what I did Thanks

My version:
PHP Code:
          $imgFileExt = array(".png",".jpg",".jpeg",".svg",".ico",".gif","x.png"); 
          
$ext substr(strrchr($fileTR'.'), 1);
if (
in_array(".$ext"$imgFileExt)) {
          echo 
"<b><img src='$directory$fileTR' height='30px' width='30px' style='margin-right:40px;' />$DispFormat<a href='$directory$fileTR'>".strtr(preg_replace("([A-Z]+)"" $0"$fileTR),$ridOfFExt)."</a>$DispFormat2</b><br />";
        } 
Yep, it's a thumbnail viewer
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Find string as value from array?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.34734 seconds with 12 queries