473,569 Members | 2,844 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best way to extract URL from random string?

If I have random and unpredictable user agent strings containing URLs, what is
the best way to extract the URL?

For example, let's say the string looks like this:

registered NYSE 943 <a href="http://netforex.net"Fo rex Trading Network
Organization </ain**@netforex.o rg

What's the best way to extract http://netforex.net ?

I have code that checks for identifiable browsers and bots, but when the agent
string has no identifiable information other than a URL, I want to grab the URL.

Here's a first crack at it:
..
..
..
[code omitted]
..
..
..
elseif (eregi("http://", $agent))
{
$agent = stristr($agent, "http://");
$agent = parse_url($agen t);
$agent = $agent['host'];
//check for subdomains
$agent_a = explode(".", $agent);
$agent_r = array_reverse($ agent_a);
$sub = count($agent_r) - 1;
$tld3 = substr($agent_r[0], 0, 3);
if (eregi("^(com|n et|org|edu|biz| gov)$", $tld3)) //common tld's
{
while ($sub 0)
{
$domain = $domain.$agent_ r[$sub].".";
$sub--;
}
$refurl = $domain.$tld3;
}
$referrer = "<a href='".$refurl ."'>".$refurl." </a>";
}
else
{
$referrer = "unknown";
}

Are there any PHP functions that will help here? How to handle sub domains?
International domains?

Thanks in advance.

Feb 9 '07 #1
5 5113
How about:

if
(preg_match('/\\b(https?|ftp| file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0
-9+&@#\/%=~_|]/i', $subject, $result)) {
$url = $result[0];
} else {
$url = "";
}

-----Original Message-----
From: deko [mailto:de**@nos pam.com]
Posted At: Friday, February 09, 2007 2:15 PM
Posted To: comp.lang.php
Conversation: Best way to extract URL from random string?
Subject: Best way to extract URL from random string?

If I have random and unpredictable user agent strings containing URLs,
what is
the best way to extract the URL?

For example, let's say the string looks like this:

registered NYSE 943 <a href="http://netforex.net"Fo rex Trading Network

Organization </ain**@netforex.o rg

What's the best way to extract http://netforex.net ?

I have code that checks for identifiable browsers and bots, but when the
agent
string has no identifiable information other than a URL, I want to grab
the URL.

Here's a first crack at it:
..
..
..
[code omitted]
..
..
..
elseif (eregi("http://", $agent))
{
$agent = stristr($agent, "http://");
$agent = parse_url($agen t);
$agent = $agent['host'];
//check for subdomains
$agent_a = explode(".", $agent);
$agent_r = array_reverse($ agent_a);
$sub = count($agent_r) - 1;
$tld3 = substr($agent_r[0], 0, 3);
if (eregi("^(com|n et|org|edu|biz| gov)$", $tld3)) //common tld's
{
while ($sub 0)
{
$domain = $domain.$agent_ r[$sub].".";
$sub--;
}
$refurl = $domain.$tld3;
}
$referrer = "<a href='".$refurl ."'>".$refurl." </a>";
}
else
{
$referrer = "unknown";
}

Are there any PHP functions that will help here? How to handle sub
domains?
International domains?

Thanks in advance.

Feb 9 '07 #2
On Feb 9, 2:15 pm, "deko" <d...@nospam.co mwrote:
Are there any PHP functions that will help here? How to handle sub domains?
International domains?

Thanks in advance.
well, you found parse_url
you might want to use regular expressions as well

$long_string = 'A HREF="http://something.else. example.com/blah/?
joe=bob"';
if ( preg_match('|([^\s"\']*://[^\s"\']*)|',$long_stri ng,$matches) )
{
$url = $matches[1]; // http://something.else.example.com/blah/?
joe=bob
$parts = parse_url($url) ;
if ( preg_match('/(.+)\.\w+\.\w+/',$parts['host'],$matches) )
echo $matches[1]; // something.else
}

Feb 9 '07 #3
Rik
On Fri, 09 Feb 2007 22:02:18 +0100, BKDotCom <bk***********@ yahoo.com
wrote:
On Feb 9, 2:15 pm, "deko" <d...@nospam.co mwrote:
>Are there any PHP functions that will help here? How to handle sub
domains?
Internationa l domains?

Thanks in advance.

well, you found parse_url
you might want to use regular expressions as well

$long_string = 'A HREF="http://something.else. example.com/blah/?
joe=bob"';
if ( preg_match('|([^\s"\']*://[^\s"\']*)|',$long_stri ng,$matches) )
Afaik protocols can only be a-z+, you don't have to capture the entire
match, and the url should have at least one character, so a little
optimised it would be:

'|[a-z]+://[^\s"\']+|i'

{
$url = $matches[1]; // http://something.else.example.com/blah/?
joe=bob
$url = $matches[0];

--
Rik Wasmus
Feb 9 '07 #4
"BKDotCom" <bk***********@ yahoo.comwrote in message
news:11******** *************@k 78g2000cwa.goog legroups.com...
On Feb 9, 2:15 pm, "deko" <d...@nospam.co mwrote:
>Are there any PHP functions that will help here? How to handle sub domains?
Internationa l domains?

Thanks in advance.

well, you found parse_url
you might want to use regular expressions as well

$long_string = 'A HREF="http://something.else. example.com/blah/?
joe=bob"';
if ( preg_match('|([^\s"\']*://[^\s"\']*)|',$long_stri ng,$matches) )
{
$url = $matches[1]; // http://something.else.example.com/blah/?
joe=bob
$parts = parse_url($url) ;
if ( preg_match('/(.+)\.\w+\.\w+/',$parts['host'],$matches) )
echo $matches[1]; // something.else
}
use regex to handle subdomains... I see!

but wouldn't the first few lines of my original code be a more efficient
starting point?
elseif (eregi("http://", $agent))
{
$agent = stristr($agent, "http://");
$agent = parse_url($agen t);
//now use preg_match() to return everything beginning with a '.' up to
the next word boundary (?)

still testing...

Feb 10 '07 #5

"Rik" <lu************ @hotmail.comwro te in message
news:op.tnh2l8s gqnv3q9@misant. ..
On Fri, 09 Feb 2007 22:02:18 +0100, BKDotCom <bk***********@ yahoo.com>
wrote:
On Feb 9, 2:15 pm, "deko" <d...@nospam.co mwrote:
>Are there any PHP functions that will help here? How to handle sub domains?
Internationa l domains?

Thanks in advance.

well, you found parse_url
you might want to use regular expressions as well

$long_string = 'A HREF="http://something.else. example.com/blah/?
joe=bob"';
if ( preg_match('|([^\s"\']*://[^\s"\']*)|',$long_stri ng,$matches) )
Afaik protocols can only be a-z+, you don't have to capture the entire
match, and the url should have at least one character, so a little
optimised it would be:

'|[a-z]+://[^\s"\']+|i'

{
$url = $matches[1]; // http://something.else.example.com/blah/?
joe=bob
$url = $matches[0];
=============== =============== =============

I've been thinking about this... see http://www.liarsscourge.com

I need to decide:

1) what TLDs I will accept
2) what protocols I will accept

so...

1 = common TLDs, including international TLDs
2 = http only

next...

-- assemble array of common/international TLDs
-- construct regex to search for TLDs in this array

developing...

Feb 10 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8617
by: Tim Smith | last post by:
I am looking to extract form element values from html, more generally I have a substring that identifies the beginning of a value and a string that identifies the end of value and I need to extract the substring. My ugly code looks like this: public static String getValue(String data, String begin, String end) { int delimPos =...
4
1709
by: Harald Massa | last post by:
Old, very old informatical problem: I want to "print" grouped data with head information, that is: eingabe= shall give: ( Braces are not important...) 'Stuttgart', '70197' --data-- ('Fernsehturm', '20')
3
5994
by: Joe | last post by:
I'm trying to extract part of html code from a tag to a tag code begins with <span class="boldyellow"><B><U> and ends with TD><TD> <img src="http://whatever/some.gif"> </TD></TR></TABLE> I was thinking of using a regular expression however I having hard time getting the desired string. I use htmlSource = urllib.urlopen("http://address/")...
10
1962
by: Rich Wallace | last post by:
Hey all, I have an XML doc that I read into a SQL Server database from an integration feed.... ----------------XML snippet ---------------- <?xml version="1.0" encoding="us-ascii"?> <!--Product data from JDEdwards--> <Root> <Root RvcDate="2004-02-03" RcvTime="14.16.03.795135">
0
4208
by: Anonieko Ramos | last post by:
ASP.NET Forms Authentication Best Practices Dr. Dobb's Journal February 2004 Protecting user information is critical By Douglas Reilly Douglas is the author of Designing Microsoft ASP.NET Applications and owner of Access Microsystems. Doug can be reached at doug@accessmicrosystems.com....
10
2657
by: Leon | last post by:
I know by default the random number generator use the time, but what is the best seed I can used in my web application? The Program generate 6 unique random numbers and load each of them in a textbox control. I need a good seed like ip address or something. 'Function to generate random numbers Public Function GetRandomNumber() As Integer
29
2881
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
1
1603
by: GS | last post by:
I need to extract sections out of a long string of about 5 to 10 KB, change any date format of dd Mmm yyyy to yyyy-mm-dd, then further from each section extract columns of tables. what is the best approach in using regex for this? I can see match and replace the dates, extract section with regex, and then for each section extract again...
0
912
by: MDSS | last post by:
I am looking for the code to extract just one record from my file and print to a Form. Spent two days on the net but can not find the answer any where. The code below works fine but not for a single record. Any ideas? Private Sub Command4_Click() ' Sub ReadRandom() Dim P As Person
0
7695
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.