Page 1 of 1

Compare string with pattern

Posted: Wed Jan 04, 2006 8:10 am
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?

Posted: Wed Jan 04, 2006 8:14 am
by feyd
regex is most often used for pattern matching. Read the regex board's tutorials written by d11wtq and its threads for details.

Posted: Wed Jan 04, 2006 8:15 am
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 ;)

Posted: Wed Jan 04, 2006 9:30 am
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

Posted: Wed Jan 04, 2006 11:47 am
by ffclan
Thanks everybody.

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