3

How can I write a regex which will replace any file name, with it's folder path, but wouldn't match url? and wouldn't match in url? 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...

1 Answer 1

5

I think this is imposible since this is also a valid directory name?

www.google.com/scripts/myscript.js

I guess only http:// could be filtered.

I don't know what you're planning to do, but maybe you could use file_exists() to check if it's a file on ur filesystem.

this question tells us how to filter a url with regex:

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

but it also states that you actually should use the filter_var() function

var_dump(filter_var('example.com', FILTER_VALIDATE_URL);
Sign up to request clarification or add additional context in comments.

3 Comments

well say it's not a vaild directory name.
Alon, you'll first have to provide a definition of an invalid directory is (in your book). We can't read your mind. Is a directory invalid when it has two .'s in it? Is it invalid when it ends with .com? Etc. etc.
www.google.com is a valid directory name on Windows 95 (!) and newer, and on all modern Unix-y systems (OS X, Linux) also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.