473,395 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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"Forex Trading Network
Organization </ain**@netforex.org

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($agent);
$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|net|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 5101
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**@nospam.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"Forex Trading Network

Organization </ain**@netforex.org

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($agent);
$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|net|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.comwrote:
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_string,$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.comwrote:
>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_string,$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*********************@k78g2000cwa.googlegro ups.com...
On Feb 9, 2:15 pm, "deko" <d...@nospam.comwrote:
>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_string,$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($agent);
//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.comwrote in message
news:op.tnh2l8sgqnv3q9@misant...
On Fri, 09 Feb 2007 22:02:18 +0100, BKDotCom <bk***********@yahoo.com>
wrote:
On Feb 9, 2:15 pm, "deko" <d...@nospam.comwrote:
>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_string,$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
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...
4
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--...
3
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...
10
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"?>...
0
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...
10
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...
29
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
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...
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.