Jump to content

[SOLVED] Extracting part of link from URL


cuffy

Recommended Posts

I hope someone can help me. I have searched for ages for a solution and cannot find a simple answer anywhere.

 

There are many scripts that will extract and display all the links from a single web-page but what I am looking for is a script that will output only part of the link.

 

For example if one of the array outputs was www.bbc.co.uk/football/1234 then all I want is 1234 to be the output and to ignore all the other parts of the url or any other url which does not start with www.bbc.co.uk/football

 

I hope someone can help - it's driving me mad.

Link to comment
Share on other sites

Do you have a defined area you want to get (ie always the last part,  always after the 3rd slash, etc)? If so, just break out into an array based on the '/' and use your different array functions to get that part.

 

If not, post back on here and let us know.

 

As for the second part, you can do string matching with strpos

Link to comment
Share on other sites

Thank you for your amazing quick response.

 

I am sorry but my ability to code php is fairly limited but I am trying !

 

The link always follows the following format; ie: "http://cgi1.igl.net/tourney/ydom/196831".

The original url where I was looking to extract the link from is found at: "http://www4.igl.net/cgi-bin/tourney/gencalendar.cgi?path=ydom"

 

I am merely looking for a function/array that will produce the numeric value of the link. In the above example "196831".

 

I was hoping that I could set a variable of something like $tourney where I can reuse the $tourney[0] is some html that I am writing.

Link to comment
Share on other sites

So I think it would be something like this:

$tourney = explode('/', url here, however you're getting it)
echo $tourney[array_size($tourney)-1];

That should give you the last value which is your number. Unfortunately, I can't test right now, but hopefully it gets you moving in the right direction.

Link to comment
Share on other sites

Thank you for your pointers.

 

Whilst I am sure you have better methods and I haven't got my variables I am sure I have something that I'll be able work around:

 

<?PHP

 

$page = 0;

$URL = "http://www4.igl.net/cgi-bin/tourney/gencalendar.cgi?path=ydom";

$page = @FOPEN($URL, "r");

 

WHILE(!FEOF($page)) {

 

    $line = FGETS($page, 255);

 

    WHILE(EREGI("HREF=\"[^\"]*\"", $line, $match)) {

    $newstring = substr($match[0], 39, 6);

    echo $newstring;

 

          PRINT("<BR>\n");

          $replace = EREG_REPLACE("\?", "\?", $match[0]);

          $line = EREG_REPLACE($replace, "", $line);

    }

}

 

FCLOSE($page);

 

?>

 

Link to comment
Share on other sites

use file_get_contents() to save the contents from the page in a variable, then extract the info you want using regular expressions such as preg_match_all().

 

here's a snippet that will fetch the page you want, extract the values and print them out on the screen, ex:

<?php

$html = file_get_contents('http://www4.igl.net/cgi-bin/tourney/gencalendar.cgi?path=ydom');
preg_match_all('/(?<=Jump: )\d+/s', $html, $matches);

foreach($matches[0] as $value)
{
echo "$value<br />";
}

?>

 

and the output will look something like this:

196791
196831
196904
196918
196929

Link to comment
Share on other sites

Thanks for all your help - it has been invaluable.

 

I have managed to extract the times for the page but for some reason I just cannot extract the text after "Location: ".

 

I know I am going wrong with the expression in the preg_match_all function but I just cannot see where.

 

What I have is:

 

$html = file_get_contents('http://www4.igl.net/cgi-bin/tourney/gencalendar.cgi?path=ydom');

preg_match_all("/Location:(.*)<\/br>/", $html, $matches);

 

but I seem to be getting no results.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.