开发者

preg_match multiple things in one regex

开发者 https://www.devze.com 2023-04-01 18:55 出处:网络
I need a regex that matches multiple things. What im trying to do, is send a mail if the url doesnt contain certain words.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消