Page 1 of 1 [ 6 posts ] 

Albinoboy
Yellow-bellied Woodpecker
Yellow-bellied Woodpecker

User avatar

Joined: 5 Aug 2010
Age: 31
Gender: Male
Posts: 70
Location: Surrey, England

02 Sep 2010, 11:37 am

(Why does my PHP suck so much? C++ was easier than this)

I need PHP to not do anything if a string does not have something in it (in this case the value stored in $ext, which stands for extension, which means I'm playing around with files)

So if file.html ends with .html then go do this, otherwise get lost.
At the moment I'm painfully listing all the files in the if, which is what I don't want to do.

Code:
if ($file != ".htaccess" && $file != "thumbs.db" && $file != "..")



kra17
Veteran
Veteran

User avatar

Joined: 14 Feb 2010
Age: 32
Gender: Male
Posts: 594
Location: Sweden

02 Sep 2010, 2:34 pm

Regular Expressions maybe?
http://www.webcheatsheet.com/php/regula ... ssions.php


_________________
:bigsmurf: :bigsmurf:


Albinoboy
Yellow-bellied Woodpecker
Yellow-bellied Woodpecker

User avatar

Joined: 5 Aug 2010
Age: 31
Gender: Male
Posts: 70
Location: Surrey, England

02 Sep 2010, 3:00 pm

So something like

Code:
if (preg_match(".".$ext."$i", $file))


Oh and in case you don't get my fancy programming there, its the same as this:

Code:
if (preg_match(".html$i", "file.html"))


So using your cheat sheet, all files ending in .html will be used, the rest ignored.

$ = at the end of string
i = case-insensitive

Right?
(I hope)



Albinoboy
Yellow-bellied Woodpecker
Yellow-bellied Woodpecker

User avatar

Joined: 5 Aug 2010
Age: 31
Gender: Male
Posts: 70
Location: Surrey, England

02 Sep 2010, 3:14 pm

Quote:
Warning: preg_match() [function.preg-match]: No ending delimiter '.' found in C:\xampplite\htdocs\core\Nav.php on line 16

Warning: preg_match() [function.preg-match]: No ending delimiter '.' found in C:\xampplite\htdocs\core\Nav.php on line 16


Success? No



kra17
Veteran
Veteran

User avatar

Joined: 14 Feb 2010
Age: 32
Gender: Male
Posts: 594
Location: Sweden

02 Sep 2010, 3:27 pm

Almost; /.html$i/


_________________
:bigsmurf: :bigsmurf:


Albinoboy
Yellow-bellied Woodpecker
Yellow-bellied Woodpecker

User avatar

Joined: 5 Aug 2010
Age: 31
Gender: Male
Posts: 70
Location: Surrey, England

02 Sep 2010, 3:32 pm

That did the trick.
Thx!