1 2 Previous Next 23 Replies Latest reply on Jul 23, 2017 9:46 PM by alexissindicic

    How to get a domain name from a url through calculation

    alexissindicic

      Hello Filemaker Community,

       

      I have a field called Referring_Page_Url with urls like :

      http://www.blabla.eu/category/page

       

      and i'd like to get only the blabla.eu in a new field called Domain_Name

       

      My first idea was to define Domain_name as a result of this calculation :

       

      Substituer ( Referring_Page_Url ; ["http:";""];["https:";""];["//";""] ; ["www.";""] )

       

      it works for the first part of the Url but not for what is after the /->

      so i get urls like

       

      blabla.eu/category/page

       

      Do you know what i could do to get rid of the /category/page

       

      i was thinking of regular expression but there may be something easier un FM ?

       

      Thanks you VERY much for your thoughts,

        • 1. Re: How to get a domain name from a url through calculation
          Jason Wood

          Let ([

          var = "http://www.blabla.eu/category/page";

          var = Substitute ( var ; [ "http://" ; "" ] ; [ "https://" ; "" ]);

          pos = Position ( var ; "/" ; 0 ; 1 );

          var = Left ( var ; pos - 1 );

          dots = PatternCount ( var ; "." );

          pos = Position ( var ; "." ; 0 ; dots - 1 );

          domain = Right ( var ; Length ( var ) - pos )

          ];

           

          domain

           

          )

           

          This assumes that you want the top 2 levels of the domain, regardless of whether a 3rd level (www) or even higher levels exist, and regardless of what they are (www, www2, or anything else).

          2 of 2 people found this helpful
          • 2. Re: How to get a domain name from a url through calculation
            alexissindicic

            Wow Jason i'm going to test this right now !

            • 3. Re: How to get a domain name from a url through calculation
              alexissindicic

              Jason you're a genius !

               

              Is there a way to handle subdomains like blog.mysupersite.eu ?

               

               

              Many thanks for your help !

              • 5. Re: How to get a domain name from a url through calculation
                Jason Wood

                You'll have to be more specific about what you want. I designed the calculation to take the top 2 parts. So if you want a subdomain, that's 3 parts. You can easily modify the calc for that, but then it will leave www too.

                 

                So perhaps the rule you want is - "Get the top 3 parts unless the 3rd part is www, in which case get the top 2 parts." Or perhaps you want the entire domain including all subdomains (regardless of the number of levels), but leave out "www." if it's at the lowest level."

                 

                It's all very easy to do in the calculation. It's just a matter of defining exactly how you want all possible situations handled.

                • 6. Re: How to get a domain name from a url through calculation
                  fmpdude

                  How about this solution?

                  (handles blog case also - as highlighted below):

                   

                  If you give me other test cases, I'll add them to verify.

                   

                  This method also handles FTP.

                   

                  (CLICK ON IMAGE IF CUT OFF ON RIGHT)

                   

                  (As currently implemented for testing...) As you tab off the Domain field, the Extracted Domain calculates.

                  HOPE THIS HELPS.

                  • 7. Re: How to get a domain name from a url through calculation
                    alexissindicic

                    Yes Jason, i think i need to be more specific.

                    Basically i want a domain name (site.com) but if there is a subdomain (other than www.) i want it also :-)

                     

                    So this is what my export would look like.

                    www.site.com/category/  --> site.com

                    genius.site.com/page  --> genius.site.com

                    blog.site.com  --> blog.site.com

                     

                    I'll try to play with the function a bit

                    • 8. Re: How to get a domain name from a url through calculation
                      alexissindicic

                      Thanks fmpdude !

                       

                      Looks great, and could it handle getting the subdomain except if it's a www ?

                       

                      Thank you so much !

                      • 9. Re: How to get a domain name from a url through calculation
                        fmpdude

                        In the screenshot above, you can see five examples without "www".

                        • 10. Re: How to get a domain name from a url through calculation
                          TomHays

                          This calculation will take any URL and pull out the domain name.  It extracts the part in between "://" and the subsequent "/".

                          As per your request, it removes the leading "www." if the domain has it.  It doesn't remove "www." from just anywhere though because you might have blogwww.example.com where you would not want the "www." removed.

                           

                          The calculation also allows for cases where the ending "/" is missing or the leading "http://" is missing.

                          This calculation doesn't care if the URL starts with "http", "https", or anything else since it only looks for the "://" part.

                           

                          Let([

                          theURL = Referring_Page_Url;

                           

                          posnColonDoubleSlash = Position(theURL; "://"; 1; 1);

                          posnStartDomain = Case(posnColonDoubleSlash > 0; posnColonDoubleSlash+3; 1);

                          posnNextSlash = Position(theURL; "/"; posnStartDomain; 1);

                          posnEndDomain = Case(posnNextSlash > 0; posnNextSlash-1; Length(theURL));

                           

                          theDomain = Middle(theURL; posnStartDomain; posnEndDomain - posnStartDomain + 1);

                          theDomainWithoutLeadingWWW =  Case(

                                     Left(theDomain; 4) = "www."; Right(theDomain; Length(theDomain) - 4);

                                     theDomain)

                          ];

                          theDomainWithoutLeadingWWW

                          )

                           

                           

                          -Tom

                          • 11. Re: How to get a domain name from a url through calculation
                            fmpdude

                            Your CF doesn't seem to work with expected URL cases like this:

                             

                            TEST DOMAIN                                                           YOUR FUNCTION RETURNS (incorrect)

                            =============================================================================

                            http://blog.domain.yourdomain.somedomain.com/   blog.domain.yourdomain.somedomain.com

                            (should be: "somdomain.com")

                            --------------------------------------------------------------------------------

                            http://somesite.com/?username=eric                        somesite.com?username=eric                     

                            (should be: "somesite.com")

                             

                            Your CF, as written, seems to only be expecting simple "www.mysite.com" type URLs and doesn't seem to handle name value pairs (URL parameters), or subdomains as I tested it.

                             

                            Check out my examples above that test OK. There may be some cases I still haven't considered also but I haven't heard anything back from the OP since this a.m..

                             

                            (My code is about four lines.)

                             

                            HOPE THIS HELPS.

                            • 12. Re: How to get a domain name from a url through calculation
                              TomHays

                              Hi fmpdude,

                               

                              Thank you for looking at my function.

                               

                              Perhaps I was not clear that this function is designed to return the entire hostname with the one exception of simple www.example.com format where it will remove the leading "www.".  This is the original poster's requirements as I understood them.

                               

                              htp://blog.oddexample.example.com/

                              yields "blog.oddexample.example.com" as expected.

                               

                              The case of URLs that have more content such as ?parameter=value and/or a # to define a page location, the function I provided handles those without issue.

                              htp://example.com/?username=tom

                              yields "example.com" as expected.

                               

                              I don't know why it would work differently for you.

                              My testing for this function is using FileMaker 11 Advanced on Mac OS X 10.6.8.

                               

                              fmpdude wrote: "(My code is about four lines.)"

                               

                              I've tried to read properly through your posts in this thread, but I was unable to see your about four line solution.  I admit that my eyes get tired, and I may have overlooked the obvious.   Please clarify what your four line solution is.

                               

                               

                              Where my simple FileMaker function does break is if the URL has "user", "password", or "port" features since a general URL can follow this format

                              scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]

                               

                              I assume that the OP wants just the host portion.  My code, as I wrote earlier, will return everything from :// to / so the user:password@host:port section of the URL will all be returned.

                               

                              -Tom

                              • 13. Re: How to get a domain name from a url through calculation
                                TomHays

                                Here is a revised version of the function that handles the full URL format

                                scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]

                                and returns just the host part.  It removes "www." if it is present at the start of the host.

                                If you want the entire host even if it has "www." then make the function return theHost instead of theDomainWithoutLeadingWWW.

                                 

                                Let([

                                theURL = Referring_Page_Url;

                                 

                                posnColonDoubleSlash = Position(theURL; "://"; 1; 1);

                                posnStartHostArea = Case(posnColonDoubleSlash > 0; posnColonDoubleSlash+3; 1);

                                posnNextSlash = Position(theURL; "/"; posnStartHostArea; 1);

                                posnEndHostArea = Case(posnNextSlash > 0; posnNextSlash-1; Length(theURL));

                                theHostAreaUPHP = Middle(theURL; posnStartHostArea; posnEndHostArea - posnStartHostArea + 1);

                                theHostAreaHP = Let([_theHAL = Substitute(theHostAreaUPHP; "@"; "¶")];GetValue(_theHAL; ValueCount(_theHAL)));

                                theHost = GetValue(Substitute(theHostAreaHP; ":"; "¶"); 1);

                                 

                                theDomainWithoutLeadingWWW =  Case(

                                           Left(theHost; 4) = "www."; Right(theHost; Length(theHost) - 4);

                                           theHost)

                                ];

                                theDomainWithoutLeadingWWW

                                )

                                 

                                -Tom

                                • 14. Re: How to get a domain name from a url through calculation
                                  TomHays

                                  TomHays wrote:

                                   

                                  The case of URLs that have more content such as ?parameter=value and/or a # to define a page location, the function I provided handles those without issue.

                                  htp://example.com/?username=tom

                                  yields "example.com" as expected.

                                   

                                  I found that my function did fail when the url lacked the / for the path but did contain ? or # for the query or fragment sections, e.g. www.example.com?param=value or www.example.com#chap2.

                                   

                                  Here is a revision of the calculation that handles those situations as well.

                                   

                                  Let([

                                  theURL = Referring_Page_Url;

                                  posnColonDoubleSlash = Position(theURL; "://"; 1; 1);

                                  posnStartHostArea = Case(posnColonDoubleSlash > 0; posnColonDoubleSlash+3; 1);

                                  posnNextSlash = Position(theURL; "/"; posnStartHostArea; 1);

                                  posnEndHostArea = Case(posnNextSlash > 0; posnNextSlash-1; Length(theURL));

                                  theHostAreaUPHP = Middle(theURL; posnStartHostArea; posnEndHostArea - posnStartHostArea + 1);

                                  theHostAreaHP = Let([_theHAL = Substitute(theHostAreaUPHP; "@"; "¶")];GetValue(_theHAL; ValueCount(_theHAL)));

                                  theHost = GetValue(Substitute(theHostAreaHP; [":"; "¶"]; ["?";"¶"]; ["#";"¶"]); 1);

                                  theDomainWithoutLeadingWWW =  Case(

                                             Left(theHost; 4) = "www."; Right(theHost; Length(theHost) - 4);

                                             theHost)

                                  ];

                                  theDomainWithoutLeadingWWW

                                  )

                                   

                                   

                                  -Tom

                                  1 2 Previous Next