PageRenderTime 106ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/icpd.php

https://github.com/mbutler/blotter
PHP | 136 lines | 74 code | 59 blank | 3 comment | 6 complexity | d9e5232125597c5b05c17f4245fc2dad MD5 | raw file
  1. <?php
  2. $usr = ""; // database user
  3. $pwd = ""; // database password
  4. $db = ""; // database name
  5. $host = ""; // usually localhost
  6. $con = mysql_connect($host, $usr, $pwd);
  7. if (!$con)
  8. {
  9. die('Could not connect: ' . mysql_error());
  10. }
  11. mysql_select_db($db, $con);
  12. $url = "http://www.iowa-city.org/icgov/apps/police/blotter.asp";
  13. $raw = file_get_contents($url);
  14. $newlines = array("\t","\n","\r","\x20\x20","\0","\x0B");
  15. $content = str_replace($newlines, "", html_entity_decode($raw));
  16. $start = strpos($content,'<table cellpadding="3" cellspacing="1" style="background-color: #333;" class="full">"');
  17. $end = strpos($content,'</table>',$start) + 8;
  18. $table = substr($content,$start,$end-$start);
  19. preg_match_all("|<tr(.*)</tr>|U",$table,$rows);
  20. foreach ($rows[0] as $row){
  21. if ((strpos($row,'<th')===false)){
  22. preg_match_all("|<td(.*)</td>|U",$row,$cells);
  23. preg_match_all("|<strong(.*)</strong>|U",$row,$names);
  24. preg_match_all("|<br(.*)</td>|U",$row,$addy);
  25. preg_match_all("|<strong(.*)</strong>|U",$row,$arrest_date);
  26. preg_match_all("|<strong(.*)</strong>|U",$row,$cop);
  27. preg_match_all("|<br />(.*)</td>|U",$row,$dob);
  28. $name = strip_tags($names[0][0]);
  29. $home_addy = strip_tags($addy[0][0]);
  30. $offense_date = strip_tags($arrest_date[0][1]);
  31. $offense_time = substr($offense_date, -5);
  32. $offense_date = substr($offense_date, 0, -7);
  33. $offense_date = dateconvert($offense_date);
  34. $birthday = strip_tags($dob[0][1]);
  35. $birthday = substr($birthday, 6);
  36. $birthday = dateconvert($birthday);
  37. $arrest_loc = strip_tags($cells[0][2]);
  38. $officer = strip_tags($cop[0][2]);
  39. $incident_num = strip_tags($cells[0][3]);
  40. $ca = strip_tags($cells[0][4]);
  41. $arrest_type = strip_tags($cells[0][5]);
  42. $charges = preg_split("/[0-9]\)/", $arrest_type);
  43. $show_charges = '';
  44. $sql="INSERT INTO arrests (name, address, dob, location, arrest_date, arrest_time, officers, ca, incident ) VALUES('$name', '$home_addy', '$birthday', '$arrest_loc', '$offense_date', '$offense_time', '$officer', '$ca', '$incident_num')";
  45. $result = mysql_query($sql);
  46. unset($charges[0]);
  47. foreach ($charges as $charge)
  48. {
  49. $show_charges .= $charge;
  50. //$show_charges .= "<br />";
  51. $sql3="INSERT INTO charges (charge, incident) VALUES('$show_charges', '$incident_num')";
  52. $result3 = mysql_query($sql3);
  53. $show_charges = '';
  54. }
  55. //echo "<strong>Name:</strong> " . $name . "<br /> " . "<strong>Birth date:</strong> " . $birthday . "<br />" . "<strong>Home address:</strong> " . $home_addy . "<br />" . "<strong>Arrest location:</strong> " . $arrest_loc . "<br />" . "<strong>Arrest date:</strong> " . $offense_date . "<br />" . "<strong>Arrest time:</strong> " . $offense_time . "<br />" . "<strong>Officer(s): </strong>" . $officer . "<br />" . "<strong>Charges:</strong> " . $show_charges . "<br /><br />";
  56. }
  57. }
  58. //echo "Congratulations! The ICPD arrest blotter for today has been added to your private database, sir.";
  59. function dateconvert($olddate)
  60. {
  61. $newdate = explode("/", $olddate);
  62. $year = $newdate[2];
  63. $month = $newdate[0];
  64. $day = $newdate[1];
  65. $zero = "0";
  66. if (strlen($month) == 1)
  67. {
  68. $month = $zero . $month;
  69. }
  70. if (strlen($day) == 1)
  71. {
  72. $day = $zero . $day;
  73. }
  74. $isodate = $year . "-" . $month . "-" . $day;
  75. return $isodate;
  76. }
  77. ?>