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("mail@mail.com", "the url doesnt contain .xml, .jpg or开发者_开发知识库 .ico", $url);
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)) {
}
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
精彩评论