Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 10-29-2003, 09:25 PM   #1 (permalink)
jimmyoctane
Registered User
 
jimmyoctane's Avatar
 
Join Date: Sep 2003
Location: California
Posts: 26
jimmyoctane is on a distinguished road
Send a message via AIM to jimmyoctane
Scanning for multiple entries in a string

I want to pull a web page into a string, and scan through to extract every URL embedded in an anchor tag into an array.

I figure the best way to do this is using a preg_match, but I'm not that great with regular expressions.

let's say I have a page like this:
PHP Code:
<html>
<
body>
some text <a href=http://www.codenewbie.com>some link</a> .
<br><br>
here is another <a href="http://www.code400.com"another link</a>.
<
br><br>
and 
another link <a href="forum.php">forum</a>. 
i can get all that into a string, but what would i use to extract only the links into an array? it would end up something like this:
PHP Code:
<?
$array
[0]="http://www.codenewbie.com";
$array[1]="http://code400.com";
$array[2]="forum.php";
?>
any help would be appreciated.

Thanks in advance
Jimmy
jimmyoctane is offline   Reply With Quote
Old 11-15-2003, 08:07 PM   #2 (permalink)
Epsilon
Regular Contributor
 
Epsilon's Avatar
 
Join Date: Mar 2003
Location: Las Vegas, NV
Posts: 127
Epsilon is on a distinguished road
There's always more than one way to do something, but here's a simple way to do it:
Code:
// Load the page into an array. The following will assign each line as an array element
$my_file = file("document.html");

// Now loop through the document lines
while ($file_line = each($my_file))
{
// Check to see if this line contains a link
   if (ereg("<a href=",$file_line[value]))
   {
      // If it does, get rid of the junk preceeding and following the URL
      str_replace('.*<a href=','',$file_line[value]);
      str_replace('</a>.*','',$file_line[value]);

      // Then push it onto a new array
      array_push($array,$file_line[value]);
   }
}
This will work but it probably isn't very thorough (since I wrote it in a hurry and I've been drinking tonight). It won't grab any more than one link from a single line, and it also will not work for links where the opening and closing tags are not on the same line, but maybe it will help to get you pointed in the right direction.
__________________
--Epsilon--
Epsilon is offline   Reply With Quote
Old 11-15-2003, 08:19 PM   #3 (permalink)
jimmyoctane
Registered User
 
jimmyoctane's Avatar
 
Join Date: Sep 2003
Location: California
Posts: 26
jimmyoctane is on a distinguished road
Send a message via AIM to jimmyoctane
Thanks for the help

I appreciate the help. I will let you know where I can take it from here.

Hey! I see your in Vegas.....Dont you drink every nite.


Thanks
Jimmy
jimmyoctane is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Resetting a table from a backup copy... metazai PHP 15 06-23-2004 12:58 PM
Table problems prendo HTML, XML, Javascript, AJAX 12 04-06-2003 05:38 AM
help me name a table sde Lounge 4 12-07-2002 12:50 PM


All times are GMT -8. The time now is 10:38 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting