Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 1st, 2012  |  syntax: None  |  size: 3.02 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. how can i make my php code shorter?
  2. $_ = NULL;
  3. $msg = "<br />[ Your Script ".$handle->file_dst_name_body." has been uploaded ]<br />";
  4. $file = $handle->file_dst_path.$handle->file_dst_name_body.'.'.$handle->file_dst_name_ext;
  5. $open = fopen($file, 'r+');
  6. $story = utf8_decode(fread($open, filesize($file)));
  7.  
  8. echo $msg.'<br />';
  9.  
  10. $sf = new TextFileReader($file);
  11. $sf->seekLine(1); // Go to line 1
  12.  
  13. echo "Total number of lines: ".$sf->countLines()."<br />";
  14. if(preg_match_all('/s{68}(.*)/m', $story, $_))
  15. {
  16.     $unique_pages = array_unique(array_map(create_function('$a', 'return ucfirst(strtolower(trim($a)));'), $_[1]));
  17.     echo "<span><strong>Pages: </strong>".count($unique_pages)."</span><br />";
  18. }
  19. $searchfor = array();
  20. $searchfor[0] = 'INT.';
  21. $searchfor[1] = 'EXT.';
  22. $searchfor[2] = 'I/E.';
  23.  
  24. // get the file contents, assuming the file to be readable (and exist)
  25. $contents = mb_convert_encoding(file_get_contents($file), "UTF-8", "auto");
  26. // escape special characters in the query
  27. $pattern = preg_quote($searchfor[0], '/');
  28. // finalize the regular expression, matching the whole line
  29. $pattern = "/^.*$pattern.*$/m";
  30.  
  31. $list = "";
  32. $numInt  = 0;
  33. // search, and store all matching occurences in $matches
  34. if(preg_match_all($pattern, $contents, $matches))
  35. {
  36.     $list = "<ol>";
  37.     foreach($matches as $match) {
  38.         $list .= "<li><div class='int'>".implode("</div></li><li><div class='int'>", $match)."</div></li>n";
  39.     }
  40.     $numInt = count($matches[0]);
  41.     $list .= "</ol>";
  42. }else{
  43.     $list = "No matches found";
  44. }
  45.  
  46. // escape special characters in the query
  47. $pattern2 = preg_quote($searchfor[1], '/');
  48. // finalize the regular expression, matching the whole line
  49. $pattern2 = "/^.*$pattern2.*$/m";
  50. $list2 = "";
  51. $numExt  = 0;
  52. // search, and store all matching occurences in $matches
  53. if(preg_match_all($pattern2, $contents, $matches2))
  54. {
  55.     $list2 = "<ol>";
  56.     foreach($matches2 as $match) {
  57.         $list2 .= "<li><div class='int'>".implode("</div></li><li><div class='int'>", $match)."</div></li>";
  58.     }
  59.     $numExt = count($matches2[0]);
  60.     $list2 .= "</ol>";
  61. }else{
  62.     $list2 = "No matches found";
  63. }
  64.  
  65. // escape special characters in the query
  66. $pattern3 = preg_quote($searchfor[2], '/');
  67. // finalize the regular expression, matching the whole line
  68. $pattern3 = "/^.*$pattern3.*$/m";
  69. $list3 = 0;
  70. $numIe = 0;
  71. // search, and store all matching occurences in $matches
  72. if(preg_match_all($pattern3, $contents, $matches3))
  73. {
  74.     $list3 = "<ol>";
  75.     foreach($matches3 as $match) {
  76.         $list3 .= "<li><div class='int'>".implode("</div></li><li><div class='int'>", $match)."</div></li>n";
  77.     }
  78.     $numIe = count($matches3[0]);
  79.     $lis3t .= "</ol>";
  80. }else{
  81.     $list3 = "No matches found";
  82. }
  83.        
  84. // search, and store all matching occurences in $matches
  85. if (preg_match_all($pattern2, $contents, $matches2)) {
  86.     $list2 = "<ol>";
  87.     foreach($matches2 as $match) {
  88.         $list2 .= "<li><div class='int'>".implode("</div></li><li><div class='int'>", $match)."</div></li>";
  89.     }
  90.     $numExt = count($matches2[0]);
  91.     $list2 .= "</ol>";
  92. }
  93. else {
  94.     $list2 = "No matches found";
  95. }