K
K
khodos_dmitry2018-02-22 21:13:03
PHP
khodos_dmitry, 2018-02-22 21:13:03

How to write a regular expression with negation?

We need a regular expression that searches from links and selects those that lead not to pages, but to pictures, for example. Those. with extension. But with an extension other than html, php, etc. Please note that pages are without extension.
I made this one:

preg_match("#\.(?!htm|html|asp|aspx|php)$#Uum", $url)

I've tried many more but I can't get the desired result.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
khodos_dmitry, 2018-02-23
@khodos_dmitry

In general, I solved the problem with two if

if (preg_match("#/[^.]*\.[^.]*$#Uui", $url)) {
  if (preg_match("#(?<!htm|html|asp|aspx|php)$#Uui", $url)) {
    continue;
  }
}

A
Alexey, 2018-02-22
@Azperin

So if you need pictures, then look for pictures

if(preg_match('/\.(jpg|png|jpeg|bmp|gif|svg)$/', $url)) {
  //картинка
} else {
  //не картинка
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question