Kristian Rafteseth
Kristian Rafteseth

Reputation: 2032

preg_match multiple things in one regex

I need a regex that matches multiple things.

What im trying to do, is send a mail if the url doesnt contain certain words. (.xml, .jpg, .ico)

My try, that didnt work:

if (!preg_match("/(\.xml)|(\.jpg)|(\.ico)/", $url))
mail("[email protected]", "the url doesnt contain .xml, .jpg or .ico", $url);

Upvotes: 2

Views: 1414

Answers (2)

genesis
genesis

Reputation: 50976

try this one

<?php 
$url = "imaeg.ic";
if (!preg_match("/(\.xml|\.jpg|\.ico)/", $url))
   die("mail sent 2");

http://sandbox.phpcode.eu/g/c340b/1

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270607

You're almost there. Enclose the three extension types in one () group separated by | and keep the . outside of it. Also, I've added a $ to the end to indicate that the file extension occurs as the last thing in the string so something like example.xml.com doesn't accidentally match.

if (!preg_match("/\.(xml|jpg|ico)$/", $url)) {

}

Upvotes: 5

Related Questions