1

I want to make a php script that will check a text file or a string if it contains a url or domain.

I would like to ask how to make a regex to match url. I would like to match the following strings:

example.com
example.com.ca
example.co.uk
www.example.com
www.example.com.ca
www.example.co.uk
m.example.com
mobile.example.com
sample-example.com
mobi.m.example.co.uk
mobi-m.example.co.uk

and all the other possible domain/url just without the http://, https://, ftp:// etc.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
JAlex
  • 29
  • 4
  • 1
    If you simply want to check if a hostname is valid you might want to take a look at [gethostbyname](http://de2.php.net/manual/en/function.gethostbyname.php). – ccKep Jan 07 '14 at 14:40
  • hey ccKep. I will update my question. I totally forgot to add this. sorry. – JAlex Jan 07 '14 at 14:46
  • @HamZa It is not duplicate with the question you regarded. Here he meant only with the protocol section of the URL. – SaidbakR Jan 07 '14 at 14:53
  • @sємsєм don't tell me that it's difficult to add one line of regex to that accepted answer ? Anyways, I don't like most questions that don't show any efforts. It's like asking me to do the OP's work. – HamZa Jan 07 '14 at 14:54
  • @HamZa In many cases, some developers deal with regex as the majority of car's drivers dealing with automobiles mechanics. i.e just the piece that the code need it. – SaidbakR Jan 07 '14 at 14:57
  • @HamZa if you don't want to answer my question then don't do it. I am new to programming. and if you don't know, before I ask a question here, I find answers to my questions first. But I don't have one, all the solutions I have didn't work. All the best answers here don't work, maybe I don't know how to put it in my script. And sorry for my question that you think it don't have efforts, I just don't know how to put my questions in mind into words. – JAlex Jan 07 '14 at 15:07
  • @JAlex I don't want to answer your question since it doesn't contain any sign that you did try something. Your question is clear, but it lacks code that you have tried/searched. I mean if I give you directly the solution, what will you learn from it ? You'll most likely copy paste it and come back again whenever you'll encounter a small problem with it. I'll admit that the task is a bit hard, I mean how will you prevent matches like `troll.lol` or `mr.me` ? Have you thought about [IDN](http://en.wikipedia.org/wiki/File:IDN-utopia-greek.jpg) ? – HamZa Jan 07 '14 at 16:06

0 Answers0