5

I'm trying to extract data (text) from an external site and put it on my site. I want to get football scores of an external site and put it on mine. I've researched and found out I can do this using Preg_Match but i just can't seem to figure out how to extract data within html tags.

For example

this is the HTML structure of an external site.

<td valign="top" align="center" class="s1"><b>Text I Want To Fetch</b></td>

How would I fetch the text within tags? Would help me out allot! THANKS!

9
  • 1
    Use the DOM library, if you have any questions with this library, feel free to come back! Commented Aug 5, 2013 at 1:04
  • +1 for quick and helpful @DaveChen :) Commented Aug 5, 2013 at 1:06
  • i am a gigantic noob and need a solid example that's identical to my issue to understand. But thanks for the quick tip! Commented Aug 5, 2013 at 1:13
  • 1
    bet you a million dollars there are API's for this, stop doing things the hard way. and not that because its one some one else's site does not automatically give you the rights to use it on yours. Commented Aug 5, 2013 at 1:31
  • I know what you are trying to say but I am not stealing content. I just want to fetch some score data and show it on my site. Commented Aug 5, 2013 at 1:36

3 Answers 3

2

You can get the content of a webpage by using file_get_contents method.

Eg:

$content = file_get_contents('http://www.source.com/page.html');

Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

<?php

$html = '<td valign="top" align="center" class="s1"><b>Text I Want To Fetch</b></td>';

$dom = new DOMDocument();
$dom->loadHTML($html);
$dom = $dom->getElementsByTagName('td'); //find td
$dom = $dom->item(0);                    //traverse the first td
$dom = $dom->getElementsByTagName('b');  //find b
$dom = $dom->item(0);                    //traverse the first b
$dom = $dom->textContent;                //get text

var_dump($dom);                          //dump it, echo, or print

Output

In this example, there weren't any other textContent, so if your HTML only has text within bold, you may use this as well:

<?php

$html = '<td valign="top" align="center" class="s1"><b>Text I Want To Fetch</b></td>';

$dom = new DOMDocument();
$dom->loadHTML($html);
$dom = $dom->textContent;

var_dump($dom);

Output

2 Comments

exactly what I need! How would I use this with an external site? Where do I say open this url?
I would listen to Dagon's comment. What is the website in question? If they don't provide an API, they probably don't want you to be doing this. And Stackoverflow is not the place to be asking these types of questions then.
0

if you're talking about using php to fetch data, then file_get_contents(url) may help; however, you can fetch data using AJAX request with Jquery too. Down here is the link to AJAX documentation: http://api.jquery.com/jquery.ajax/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.