-2

How can I extract the domain name from some text using PHP?

For example, my text should be something like following:

[amr]http://www.example1.com[amr]4[amr]af[amrlf]7
[amr]http://www.example1.com[amr]2[amr]ad[amrlf]22
[amr]http://www.example1.net[amr]1[amr]ad[amrlf]26
[amr]http://www.example1.info[amr]3[amr]af[amrlf]31

Now I just want to get the domain name from this without http:// or www.

Can anyone help me on this?

Jayesh Ambali
  • 211
  • 6
  • 24
  • Your text doesn't make any sense. What are "[amr]" and "[amrlf]" supposed to represent? – Colin Fine Dec 13 '11 at 13:07
  • possible duplicate of [Simple regex for domain names](http://stackoverflow.com/questions/7742010/simple-regex-for-domain-names) – mario Dec 13 '11 at 13:23
  • This just a small portion of a text from a file and it can be anything like [amr], 2[amr],ad[amrlf],22[amr] and my requirement is to get the domain name from that text. – Jayesh Ambali Dec 13 '11 at 13:39
  • @ Colin Fine That can be anything(special characters, numeric or alphabets) after the valid domain and I guess it makes sense. – Jayesh Ambali Sep 07 '12 at 07:04
  • @ mario The link you mentioned here is not the duplicate of my question. Please do check the question before making comments – Jayesh Ambali Sep 07 '12 at 07:07

2 Answers2

0

Here is another solution :)

$url  = 'http://google.com?arg=value#anchor';
print_r(parse_url($url));
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
0

How about:

$str = '[amr]http://www.example1.com[amr]4[amr]af[amrlf]7[amr]http://www.example1.com[amr]2[amr]ad[amrlf]22[amr]http://www.example1.net[amr]1[amr]ad[amrlf]26[amr]http://www.example1.info[amr]3[amr]af[amrlf]31';
preg_match_all('#http://(?:www\.)?(.*?)\[amr#', $str, $matches);
print_r($matches[1]);

output:

Array
(
    [0] => example1.com
    [1] => example1.com
    [2] => example1.net
    [3] => example1.info
)
Toto
  • 89,455
  • 62
  • 89
  • 125
  • Thank you very much M42.This regular expression is working fine with the above string, but not with my file of text(This text is from a file and Iam reading the file contents to a variable). I just copied the portion of the string in my question. Do I need to upload the file ? – Jayesh Ambali Dec 13 '11 at 13:42
  • If the file is not too big, you can read it in a string and then apply the `preg_match_all` command on this string. If it's too big, process line by line. – Toto Dec 13 '11 at 13:47
  • File is not so big, just 2 MB of size. Yes I did exactly what you said in your comment, I took the file contents as a string(using file_get_contents()) and applied the regular expression provided by you. But no luck. Once again thanks for giving reply. About file upload in the first comment : I asked whether stackoverflow has any option to upload the file with the question, so that I can upload that file to give more clarification. – Jayesh Ambali Dec 13 '11 at 13:52
  • What is not working? What did you get? Edit your question and say what you obtain. – Toto Dec 13 '11 at 13:58
  • It is working fine now. Earlier the code was like this $file = file_get_contents('file2.txt'); preg_match_all('#http://(?:www\.)?(.*?)\[amr#', $file, $matches); echo '
    ';
         print_r($matches[1]); and this was not working for me.
             
         Then I modified the code by giving whole string to $file in code itself and it is working fine now.
    – Jayesh Ambali Dec 13 '11 at 14:10