Compare string with pattern

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ffclan
Forum Newbie
Posts: 10
Joined: Fri Dec 30, 2005 3:59 am

Compare string with pattern

Post by ffclan »

Hey,

How can i compare a string with a pattern?

eg: i have in a table in a database with a column consisting of data about file types. My php script queries the data in that colmn and i am able to display it.

How can i make it that if the data is "image/*" [where the * is a wildcard (aint it?) can be anything (eg: image/jpeg)] to echo "Image File".

This is how i done it but not working as im not sure of the way it is supposed to be coded.

Code: Select all

if ($type == "Image/*")
echo "Image File";
Im 100% sure my problem lies in the first line. Note that $type already has a value

Anyone care to help out?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

regex is most often used for pattern matching. Read the regex board's tutorials written by d11wtq and its threads for details.
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

regex, i can't write regex but heres a link to help you

http://us2.php.net/manual/en/reference. ... syntax.php

combine that with preg_match or some function like.

EDIT: feyd's back with his lightning fast fingers ;)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You could avoid using regular expressions for this by using substr() (combined with strtolower() if you don't know what case the text will be in):

Code: Select all

if (strtolower(substr($type, 0, 6)) == 'image/') {
    echo 'Image File';
}
Mac
ffclan
Forum Newbie
Posts: 10
Joined: Fri Dec 30, 2005 3:59 am

Post by ffclan »

Thanks everybody.

twigletmac, your script worked like a charm. Thanks alot.
Post Reply