Regex for file names without url?

How to write a regular expression that will replace any file name, with it the path to the folder, but does not match the URL? and will not match the URL in ? For example, it should match:

/images/something.png
content/scripts/myscript.js
image.gif
/1.jpg

But should not match:

http://www.google.com/images/something.png
www.google.com/scripts/myscript.js
http://site.com/?img=/image.png
http://site.com/?img=/scripts/somescript.js

Thanks...

+3
source share
1 answer

I think this is not possible, since it is also a valid directory name?

www.google.com/scripts/myscript.js

I assume that only http: // can be filtered.

I do not know what you plan to do, but perhaps you can use file_exists () to check if there is a file in the ur file system.

, URL :

$text = preg_replace("
  #((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)#ie",
  "'<a href=\"$1\" target=\"_blank\">$3</a>$4'",
  $text
);

, filter_var()

var_dump(filter_var('example.com', FILTER_VALIDATE_URL);
+5

Source: https://habr.com/ru/post/1721676/


All Articles