Advertisement
Guest User

Untitled

a guest
Jul 12th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. <?php
  2.  
  3. function GetPage($URL)
  4. {
  5. #Get the source content of the URL
  6. $source = file_get_contents($URL);
  7.  
  8. #Extract the raw URl from the current one
  9. $scheme = parse_url($URL, PHP_URL_SCHEME); //Ex: http
  10. $host = parse_url($URL, PHP_URL_HOST); //Ex: www.google.com
  11. $raw_url = $scheme . '://' . $host; //Ex: http://www.google.com
  12.  
  13. #Replace the relative link by an absolute one
  14. $relative = array();
  15. $absolute = array();
  16.  
  17. #String to search
  18. $relative[0] = '/src="\//';
  19. $relative[1] = '/href="\//';
  20.  
  21. #String to remplace by
  22. $absolute[0] = 'src="' . $raw_url . '/';
  23. $absolute[1] = 'href="' . $raw_url . '/';
  24.  
  25. $source = preg_replace($relative, $absolute, $source); //Ex: src="/image/google.png" to src="http://www.google.com/image/google.png"
  26.  
  27. return $source;
  28. }
  29.  
  30. function SaveToDB($source)
  31. {
  32. #Connect to the DB
  33. $db = mysql_connect('localhost', 'root', '');
  34.  
  35. #Select the DB name
  36. mysql_select_db('test');
  37.  
  38. #Ask for UTF-8 encoding
  39. mysql_query("SET NAMES 'utf8'");
  40.  
  41. #Escape special chars
  42. $source = mysql_real_escape_string($source);
  43.  
  44. #Set the Query
  45. $query = "INSERT INTO website (source) VALUES ('$source')"; //Save it in a text row, that's it...
  46.  
  47. #Run the query
  48. mysql_query($query);
  49.  
  50. #Close the connection
  51. mysql_close($db);
  52. }
  53.  
  54. $source = GetPage('http://www.google.com');
  55.  
  56. SaveToDB($source);
  57.  
  58. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement