{"id":199,"date":"2007-05-12T16:57:11","date_gmt":"2007-05-12T14:57:11","guid":{"rendered":" http:\/\/www.nsonic.de\/blog\/2007\/05\/check-for-valid-url\/"},"modified":"2007-05-12T16:57:13","modified_gmt":"2007-05-12T14:57:13","slug":"check-for-valid-url","status":"publish","type":"post","link":"https:\/\/www.nsonic.de\/blog\/2007\/05\/check-for-valid-url\/","title":{"rendered":"Check for valid URL"},"content":{"rendered":"<p>Parsing out a URL\/Web Address\/Email Address<br \/>\nGetting a list of URLs from a common sentence, or a line of text:<br \/>\nWritten by: Nelson Williams<br \/>\nThis tip helps you to retreive a StringList full of URLs,<br \/>\nEMail addresses, etc.<br \/>\n<!--more--><br \/>\n[code lang=&#8221;delphi&#8221;]function GetURLs(line: string): TStringList;<br \/>\nvar<br \/>\n  i      : integer;<br \/>\n  posURL,<br \/>\n  s      : string;<br \/>\nbegin<br \/>\n   \/\/Obviously, first we create a TStringList.<br \/>\n  Result := TStringList.Create;<br \/>\n   \/\/We must make a copy of our string, since we need a variant section of a string<br \/>\n   \/\/to work with out loop. S is just that.<br \/>\n  s := line;<br \/>\n   \/\/This is our counter.<br \/>\n  i := 0;<br \/>\n   \/\/If the string is smaller than 5 characters, its not going to be a URL, that is<br \/>\n   \/\/for sure.<br \/>\n  if Length(s) < 5 then exit;\n   \/\/We begin our repeat..unitl loop, using I as the counter of spaces.\n   \/\/This is a classic string cracking loop, where a a sentence is broken into its words.\n   \/\/Here, we are breaking the string into words, and checking each word in the string\n   \/\/if it has any properties that qualify it to be a url.\n  repeat\n     \/\/Get the first space.\n    i := Pos(' ', s);\n     \/\/If a space does exist, in fact, then we carve out the possible link.\n    if i > 0 then<br \/>\n    begin<br \/>\n       \/\/This is the copy method.<br \/>\n      posURL := Copy(s, 1, i-1);<br \/>\n       \/\/Decrease the s, dynamic string, by whatever we just carved out, so next time<br \/>\n       \/\/we loop around, we dont have to worry about it. Just use copy to remove the<br \/>\n       \/\/value equivalent to posURL from the main string.<br \/>\n      s := Copy(s, i+1, Length(s)-i);<br \/>\n       \/\/Call the function below, and check if the word is actually a link.<br \/>\n      if isURL(posURL) then<br \/>\n          \/\/if the word indeed is a ling, then add it to the string list.<br \/>\n         Result.Add(posURL);<br \/>\n     \/\/The expressions within this begin..end section do the same as above, but this<br \/>\n     \/\/is called if only one word\/token remains in the s string.<br \/>\n    end else<br \/>\n    begin<br \/>\n      PosURL := s;<br \/>\n      if isURL(posURL) then<br \/>\n         Result.Add(posURL);<br \/>\n      Break;<br \/>\n    end;<br \/>\n   \/\/if s is empty, stop the loop.<br \/>\n  until Length(s) = 0;<br \/>\n   \/\/This little section will free the TStringList if it contains no items, and set it<br \/>\n   \/\/to nil.<br \/>\n  if Result.Count = 0 then<br \/>\n     begin<br \/>\n       Result.Free;<br \/>\n       Result := nil;<br \/>\n     end;<br \/>\nend;<br \/>\n{ This function does the actual checking for the token being a URL. }<br \/>\nfunction IsURL(s: string): Boolean;<br \/>\nvar<br \/>\n  i: integer;<br \/>\nbegin<br \/>\n   \/\/From the top, make the result false.<br \/>\n  Result := False;<br \/>\n   \/\/Again, if the text of the token is less than 5, its just not going<br \/>\n   \/\/to be a URL.<br \/>\n  if Length(s) < 5 then exit;\n   \/\/If the token contains a period at the end, or two periods combined, \n   \/\/its also not going to be a url.\n  if (s[Length(s)] = '.')  or (Pos('..', s) > 0) then exit;<br \/>\n   \/\/Now we check for bogus characters, Anything between 33 and 126 is valid,<br \/>\n   \/\/Above or below, are not valid url characters.<br \/>\n  for i := 1 to Length(s) do<br \/>\n    if (Ord(s[i]) < 33) or (Ord(s[i]) > 126) then exit;<br \/>\n   \/\/The next few things check for individual characteristics of various stringd.<br \/>\n  if (Pos(&#8216;www.&#8217;,LowerCase(s)) = 1) or (Pos(&#8216;news:&#8217;, LowerCase(s)) = 1) and<br \/>\n     (Length(s) > 6) then<br \/>\n  begin<br \/>\n    Result := True;<br \/>\n    Exit;<br \/>\n  end;<br \/>\n  if (Length(s) > 12) or (Pos(&#8216;mailto:&#8217;, LowerCase(s)) = 1) and<br \/>\n     (Pos(&#8216;@&#8217;, s) > 1) and (Pos(&#8216;.&#8217;, s) > 4) and (Pos(&#8216;.&#8217;, s) > (Pos(&#8216;@&#8217;, s) +1)) then<br \/>\n  begin<br \/>\n    Result := True;<br \/>\n    Exit;<br \/>\n  end;<br \/>\n  if (Pos(&#8216;http::\/\/&#8217;, LowerCase(s)) > 0) or (Pos(&#8216;ftp:\/\/&#8217;, LowerCase(s)) > 0) and<br \/>\n     (Length(s) > 10) and (Pos(&#8216;.&#8217;, s) > 7) then<br \/>\n  begin<br \/>\n    Result := True;<br \/>\n    Exit;<br \/>\n  end;<br \/>\nend;<br \/>\n[\/code]<\/p>\n<p>[tags]Delphi, Network, Internet[\/tags]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Parsing out a URL\/Web Address\/Email Address Getting a list of URLs from a common sentence, or a line of text: Written by: Nelson&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[11],"tags":[75,106,116],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p71Tml-3d","_links":{"self":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts\/199"}],"collection":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/comments?post=199"}],"version-history":[{"count":0,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts\/199\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/media?parent=199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/categories?post=199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/tags?post=199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}