-1

I tried to load a specific div of an external webpage into a specific div of my website So I have used preg_match function to get part of the code. but I think that I write wrong pattern. this is my code:

<?php
include("simple_html_dom.php");
$html = new simple_html_dom();


   $url = "http://something.com/";
   $page_all = file_get_contents($url); 
   preg_match('#<table id="table1"><table id="table1"><table id="table2">(.*)</table></table></table>#ms', $page_all, $div_array);

   echo ($div_array[0]);
?>

I want to get content of the second table with id=table2 in http://somthing.com then it was'nt have any result.

Meghdad Hadidi
  • 1,093
  • 13
  • 28
  • 2
    [Have you tried using an XML parser instead?](http://stackoverflow.com/a/1732454/476) Seriously. – deceze Dec 13 '11 at 06:40
  • @deceze do you expect that a xml parser reads a document with more than one html tag? By the way if you don't own this page and have not ask the owner of the page to use parts of it, this is criminal what you want to do. – rekire Dec 13 '11 at 07:38
  • 1
    You already require and instantiate simple_html_dom in your script. Why don't you use it to find the elements you're looking for? – VolkerK Dec 13 '11 at 07:55
  • @rekire http://mesghal.com is a site that publish Exchange rates and gold prices daily. I Want to extract gold prices from this site, and apply my own css to it. – Meghdad Hadidi Dec 13 '11 at 08:03
  • @VolkerK please see my question. I have used `simple_html_dom.php` in my code `include("simple_html_dom.php"); $html = new simple_html_dom(); ` – Meghdad Hadidi Dec 13 '11 at 08:06
  • ...and where do you use it? Apparently nowhere as there is only one occurence of `$html` in the code snippet and that's where you instantiate the simple_html_dom object. – VolkerK Dec 13 '11 at 08:08
  • @rekire Sorry, not sure what you're getting at. – deceze Dec 13 '11 at 08:22
  • @VolkerK yes you're right, so please explain that how can I use `simple_html_dom` in this example? – Meghdad Hadidi Dec 13 '11 at 08:30
  • Just take a look at a simple_html_dom tutorial, e.g. http://net.tutsplus.com/tutorials/php/html-parsing-and-screen-scraping-with-the-simple-html-dom-library/ – VolkerK Dec 13 '11 at 08:32
  • 1
    @deceze the source code of that linked page is trash. XML parsing is impossible. – rekire Dec 13 '11 at 08:36
  • 1
    @Meghdad Hadidi I don't expect that the owner of that page wants that you use his data. That is why I won't help you directly. Look for some webservices to get the data legally. – rekire Dec 13 '11 at 08:37
  • @rekire this site is just an example at all. I want to use this way to extract data from sites that haven't `RSS` or site like: http://timaanddate.com that can not organize the `RSS` with my own `CSS`. – Meghdad Hadidi Dec 13 '11 at 09:10

1 Answers1

1

Some characters in regular expressions have special meaning, so you have to escape them. Special characters are:

. \ + * ? [ ^ ] $ ( ) { } = ! < > | :

You can use preg_quote to escape them automatically

Greenisha
  • 1,417
  • 10
  • 14