Search for code in your directory online


/ Published in: PHP
Save to your folder(s)

This script searches the files in the same directory as the script for the particular code that you are looking for. For example say I want to know which files contain a function call, I can simply use this to do a search online so i can quickly adjust that file.


Copy this code and paste it in your HTML
  1. <head>
  2. <!-- Don't adjust any javascript code -->
  3. <script type="text/javascript" >
  4. // -------------------------------------------------------------------
  5. // Switch Content Script- By Dynamic Drive, available at: http://www.dynamicdrive.com
  6. // Created: Jan 5th, 2007
  7. // April 5th, 07: Added ability to persist content states by x days versus just session only
  8. // March 27th, 08': Added ability for certain headers to get its contents remotely from an external file via Ajax (2 variables below to customize)
  9. // -------------------------------------------------------------------
  10.  
  11. var switchcontent_ajax_msg='<em>Loading Ajax content...</em>' //Customize message to show while fetching Ajax content (if applicable)
  12. var switchcontent_ajax_bustcache=true //Bust cache and refresh fetched Ajax contents when page is reloaded/ viewed again?
  13.  
  14. function switchcontent(className, filtertag){
  15. this.className=className
  16. this.collapsePrev=false //Default: Collapse previous content each time
  17. this.persistType="none" //Default: Disable persistence
  18. //Limit type of element to scan for on page for switch contents if 2nd function parameter is defined, for efficiency sake (ie: "div")
  19. this.filter_content_tag=(typeof filtertag!="undefined")? filtertag.toLowerCase() : ""
  20. this.ajaxheaders={} //object to hold path to ajax content for corresponding header (ie: ajaxheaders["header1"]='external.htm')
  21. }
  22.  
  23. switchcontent.prototype.setStatus=function(openHTML, closeHTML){ //PUBLIC: Set open/ closing HTML indicator. Optional
  24. this.statusOpen=openHTML
  25. this.statusClosed=closeHTML
  26. }
  27.  
  28. switchcontent.prototype.setColor=function(openColor, closeColor){ //PUBLIC: Set open/ closing color of switch header. Optional
  29. this.colorOpen=openColor
  30. this.colorClosed=closeColor
  31. }
  32.  
  33. switchcontent.prototype.setPersist=function(bool, days){ //PUBLIC: Enable/ disable persistence. Default is false.
  34. if (bool==true){ //if enable persistence
  35. if (typeof days=="undefined") //if session only
  36. this.persistType="session"
  37. else{ //else if non session persistent
  38. this.persistType="days"
  39. this.persistDays=parseInt(days)
  40. }
  41. }
  42. else
  43. this.persistType="none"
  44. }
  45.  
  46. switchcontent.prototype.collapsePrevious=function(bool){ //PUBLIC: Enable/ disable collapse previous content. Default is false.
  47. this.collapsePrev=bool
  48. }
  49.  
  50. switchcontent.prototype.setContent=function(index, filepath){ //PUBLIC: Set path to ajax content for corresponding header based on header index
  51. this.ajaxheaders["header"+index]=filepath
  52. }
  53.  
  54. switchcontent.prototype.sweepToggle=function(setting){ //PUBLIC: Expand/ contract all contents method. (Values: "contract"|"expand")
  55. if (typeof this.headers!="undefined" && this.headers.length>0){ //if there are switch contents defined on the page
  56. for (var i=0; i<this.headers.length; i++){
  57. if (setting=="expand")
  58. this.expandcontent(this.headers[i]) //expand each content
  59. else if (setting=="contract")
  60. this.contractcontent(this.headers[i]) //contract each content
  61. }
  62. }
  63. }
  64.  
  65.  
  66. switchcontent.prototype.defaultExpanded=function(){ //PUBLIC: Set contents that should be expanded by default when the page loads (ie: defaultExpanded(0,2,3)). Persistence if enabled overrides this setting.
  67. var expandedindices=[] //Array to hold indices (position) of content to be expanded by default
  68. //Loop through function arguments, and store each one within array
  69. //Two test conditions: 1) End of Arguments array, or 2) If "collapsePrev" is enabled, only the first entered index (as only 1 content can be expanded at any time)
  70. for (var i=0; (!this.collapsePrev && i<arguments.length) || (this.collapsePrev && i==0); i++)
  71. expandedindices[expandedindices.length]=arguments[i]
  72. this.expandedindices=expandedindices.join(",") //convert array into a string of the format: "0,2,3" for later parsing by script
  73. }
  74.  
  75.  
  76. //PRIVATE: Sets color of switch header.
  77.  
  78. switchcontent.prototype.togglecolor=function(header, status){
  79. if (typeof this.colorOpen!="undefined")
  80. header.style.color=status
  81. }
  82.  
  83.  
  84. //PRIVATE: Sets status indicator HTML of switch header.
  85.  
  86. switchcontent.prototype.togglestatus=function(header, status){
  87. if (typeof this.statusOpen!="undefined")
  88. header.firstChild.innerHTML=status
  89. }
  90.  
  91.  
  92. //PRIVATE: Contracts a content based on its corresponding header entered
  93.  
  94. switchcontent.prototype.contractcontent=function(header){
  95. var innercontent=document.getElementById(header.id.replace("-title", "")) //Reference content container for this header
  96. innercontent.style.display="none"
  97. this.togglestatus(header, this.statusClosed)
  98. this.togglecolor(header, this.colorClosed)
  99. }
  100.  
  101.  
  102. //PRIVATE: Expands a content based on its corresponding header entered
  103.  
  104. switchcontent.prototype.expandcontent=function(header){
  105. var innercontent=document.getElementById(header.id.replace("-title", ""))
  106. if (header.ajaxstatus=="waiting"){//if this is an Ajax header AND remote content hasn't already been fetched
  107. switchcontent.connect(header.ajaxfile, header)
  108. }
  109. innercontent.style.display="block"
  110. this.togglestatus(header, this.statusOpen)
  111. this.togglecolor(header, this.colorOpen)
  112. }
  113.  
  114. // -------------------------------------------------------------------
  115. // PRIVATE: toggledisplay(header)- Toggles between a content being expanded or contracted
  116. // If "Collapse Previous" is enabled, contracts previous open content before expanding current
  117. // -------------------------------------------------------------------
  118.  
  119. switchcontent.prototype.toggledisplay=function(header){
  120. var innercontent=document.getElementById(header.id.replace("-title", "")) //Reference content container for this header
  121. if (innercontent.style.display=="block")
  122. this.contractcontent(header)
  123. else{
  124. this.expandcontent(header)
  125. if (this.collapsePrev && typeof this.prevHeader!="undefined" && this.prevHeader.id!=header.id) // If "Collapse Previous" is enabled and there's a previous open content
  126. this.contractcontent(this.prevHeader) //Contract that content first
  127. }
  128. if (this.collapsePrev)
  129. this.prevHeader=header //Set current expanded content as the next "Previous Content"
  130. }
  131.  
  132.  
  133. // -------------------------------------------------------------------
  134. // PRIVATE: collectElementbyClass()- Searches and stores all switch contents (based on shared class name) and their headers in two arrays
  135. // Each content should carry an unique ID, and for its header, an ID equal to "CONTENTID-TITLE"
  136. // -------------------------------------------------------------------
  137.  
  138. switchcontent.prototype.collectElementbyClass=function(classname){ //Returns an array containing DIVs with specified classname
  139. var classnameRE=new RegExp("(^|\\s+)"+classname+"($|\\s+)", "i") //regular expression to screen for classname within element
  140. this.headers=[], this.innercontents=[]
  141. if (this.filter_content_tag!="") //If user defined limit type of element to scan for to a certain element (ie: "div" only)
  142. var allelements=document.getElementsByTagName(this.filter_content_tag)
  143. else //else, scan all elements on the page!
  144. var allelements=document.all? document.all : document.getElementsByTagName("*")
  145. for (var i=0; i<allelements.length; i++){
  146. if (typeof allelements[i].className=="string" && allelements[i].className.search(classnameRE)!=-1){
  147. if (document.getElementById(allelements[i].id+"-title")!=null){ //if header exists for this inner content
  148. this.headers[this.headers.length]=document.getElementById(allelements[i].id+"-title") //store reference to header intended for this inner content
  149. this.innercontents[this.innercontents.length]=allelements[i] //store reference to this inner content
  150. }
  151. }
  152. }
  153. }
  154.  
  155.  
  156. //PRIVATE: init()- Initializes Switch Content function (collapse contents by default unless exception is found)
  157.  
  158. switchcontent.prototype.init=function(){
  159. var instanceOf=this
  160. this.collectElementbyClass(this.className) //Get all headers and its corresponding content based on shared class name of contents
  161. if (this.headers.length==0) //If no headers are present (no contents to switch), just exit
  162. return
  163. //If admin has changed number of days to persist from current cookie records, reset persistence by deleting cookie
  164. if (this.persistType=="days" && (parseInt(switchcontent.getCookie(this.className+"_dtrack"))!=this.persistDays))
  165. switchcontent.setCookie(this.className+"_d", "", -1) //delete cookie
  166. // Get ids of open contents below. Four possible scenerios:
  167. // 1) Session only persistence is enabled AND corresponding cookie contains a non blank ("") string
  168. // 2) Regular (in days) persistence is enabled AND corresponding cookie contains a non blank ("") string
  169. // 3) If there are contents that should be enabled by default (even if persistence is enabled and this IS the first page load)
  170. // 4) Default to no contents should be expanded on page load ("" value)
  171. var opencontents_ids=(this.persistType=="session" && switchcontent.getCookie(this.className)!="")? ','+switchcontent.getCookie(this.className)+',' : (this.persistType=="days" && switchcontent.getCookie(this.className+"_d")!="")? ','+switchcontent.getCookie(this.className+"_d")+',' : (this.expandedindices)? ','+this.expandedindices+',' : ""
  172. for (var i=0; i<this.headers.length; i++){ //BEGIN FOR LOOP
  173. if (typeof this.ajaxheaders["header"+i]!="undefined"){ //if this is an Ajax header
  174. this.headers[i].ajaxstatus='waiting' //two possible statuses: "waiting" and "loaded"
  175. this.headers[i].ajaxfile=this.ajaxheaders["header"+i]
  176. }
  177. if (typeof this.statusOpen!="undefined") //If open/ closing HTML indicator is enabled/ set
  178. this.headers[i].innerHTML='<span class="status"></span>'+this.headers[i].innerHTML //Add a span element to original HTML to store indicator
  179. if (opencontents_ids.indexOf(','+i+',')!=-1){ //if index "i" exists within cookie string or default-enabled string (i=position of the content to expand)
  180. this.expandcontent(this.headers[i]) //Expand each content per stored indices (if ""Collapse Previous" is set, only one content)
  181. if (this.collapsePrev) //If "Collapse Previous" set
  182. this.prevHeader=this.headers[i] //Indicate the expanded content's corresponding header as the last clicked on header (for logic purpose)
  183. }
  184. else //else if no indices found in stored string
  185. this.contractcontent(this.headers[i]) //Contract each content by default
  186. this.headers[i].onclick=function(){instanceOf.toggledisplay(this)}
  187. } //END FOR LOOP
  188. switchcontent.dotask(window, function(){instanceOf.rememberpluscleanup()}, "unload") //Call persistence method onunload
  189. }
  190.  
  191.  
  192. // -------------------------------------------------------------------
  193. // PRIVATE: rememberpluscleanup()- Stores the indices of content that are expanded inside session only cookie
  194. // If "Collapse Previous" is enabled, only 1st expanded content index is stored
  195. // -------------------------------------------------------------------
  196.  
  197. //Function to store index of opened ULs relative to other ULs in Tree into cookie:
  198. switchcontent.prototype.rememberpluscleanup=function(){
  199. //Define array to hold ids of open content that should be persisted
  200. //Default to just "none" to account for the case where no contents are open when user leaves the page (and persist that):
  201. var opencontents=new Array("none")
  202. for (var i=0; i<this.innercontents.length; i++){
  203. //If persistence enabled, content in question is expanded, and either "Collapse Previous" is disabled, or if enabled, this is the first expanded content
  204. if (this.persistType!="none" && this.innercontents[i].style.display=="block" && (!this.collapsePrev || (this.collapsePrev && opencontents.length<2)))
  205. opencontents[opencontents.length]=i //save the index of the opened UL (relative to the entire list of ULs) as an array element
  206. this.headers[i].onclick=null //Cleanup code
  207. }
  208. if (opencontents.length>1) //If there exists open content to be persisted
  209. opencontents.shift() //Boot the "none" value from the array, so all it contains are the ids of the open contents
  210. if (typeof this.statusOpen!="undefined")
  211. this.statusOpen=this.statusClosed=null //Cleanup code
  212. if (this.persistType=="session") //if session only cookie set
  213. switchcontent.setCookie(this.className, opencontents.join(",")) //populate cookie with indices of open contents: classname=1,2,3,etc
  214. else if (this.persistType=="days" && typeof this.persistDays=="number"){ //if persistent cookie set instead
  215. switchcontent.setCookie(this.className+"_d", opencontents.join(","), this.persistDays) //populate cookie with indices of open contents
  216. switchcontent.setCookie(this.className+"_dtrack", this.persistDays, this.persistDays) //also remember number of days to persist (int)
  217. }
  218. }
  219.  
  220.  
  221. // -------------------------------------------------------------------
  222. // A few utility functions below:
  223. // -------------------------------------------------------------------
  224.  
  225.  
  226. switchcontent.dotask=function(target, functionref, tasktype){ //assign a function to execute to an event handler (ie: onunload)
  227. var tasktype=(window.addEventListener)? tasktype : "on"+tasktype
  228. if (target.addEventListener)
  229. target.addEventListener(tasktype, functionref, false)
  230. else if (target.attachEvent)
  231. target.attachEvent(tasktype, functionref)
  232. }
  233.  
  234. switchcontent.connect=function(pageurl, header){
  235. var page_request = false
  236. var bustcacheparameter=""
  237. if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
  238. try {
  239. page_request = new ActiveXObject("Msxml2.XMLHTTP")
  240. }
  241. catch (e){
  242. try{
  243. page_request = new ActiveXObject("Microsoft.XMLHTTP")
  244. }
  245. catch (e){}
  246. }
  247. }
  248. else if (window.XMLHttpRequest) // if Mozilla, Safari etc
  249. page_request = new XMLHttpRequest()
  250. else
  251. return false
  252. page_request.onreadystatechange=function(){switchcontent.loadpage(page_request, header)}
  253. if (switchcontent_ajax_bustcache) //if bust caching of external page
  254. bustcacheparameter=(pageurl.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
  255. page_request.open('GET', pageurl+bustcacheparameter, true)
  256. page_request.send(null)
  257. }
  258.  
  259. switchcontent.loadpage=function(page_request, header){
  260. var innercontent=document.getElementById(header.id.replace("-title", "")) //Reference content container for this header
  261. innercontent.innerHTML=switchcontent_ajax_msg //Display "fetching page message"
  262. if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
  263. innercontent.innerHTML=page_request.responseText
  264. header.ajaxstatus="loaded"
  265. }
  266. }
  267.  
  268. switchcontent.getCookie=function(Name){
  269. var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
  270. if (document.cookie.match(re)) //if cookie found
  271. return document.cookie.match(re)[0].split("=")[1] //return its value
  272. return ""
  273. }
  274.  
  275. switchcontent.setCookie=function(name, value, days){
  276. if (typeof days!="undefined"){ //if set persistent cookie
  277. var expireDate = new Date()
  278. var expstring=expireDate.setDate(expireDate.getDate()+days)
  279. document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()
  280. }
  281. else //else if this is a session only cookie
  282. document.cookie = name+"="+value
  283. }
  284.  
  285. </script>
  286.  
  287. <style type="text/css">
  288.  
  289. /*Style sheet used for demo. Remove if desired*/
  290. .handcursor{
  291. cursor:hand;
  292. cursor:pointer;
  293. }
  294.  
  295. </style>
  296. </head>
  297. <body>
  298.  
  299. <?php
  300. if(isset($_POST['searchvar']))
  301. {
  302.  
  303. echo '<div align="right"><A HREF="codesearch.php">Search Again?</A></div>';
  304. echo '<div><a href="javascript:bobexample.sweepToggle(\'contract\')">Collapse All</a> | <a href="javascript:bobexample.sweepToggle(\'expand\')">Expand All</a></div>';
  305. $findme = stripslashes($_POST['searchvar']);
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312. $dirarrays = Array("./");
  313.  
  314.  
  315. foreach($dirarrays as $dir)
  316. {
  317.  
  318. $files2 = scandir($dir, 1);
  319.  
  320. $count = 0;
  321. foreach($files2 as $thefile)
  322. {
  323. $pieces = explode(".",$thefile);
  324. if(count($pieces) >= 2)
  325. {
  326.  
  327.  
  328. switch($pieces[1])
  329. {
  330. case "php":
  331.  
  332. $lines = file($dir.'/'.$thefile);
  333.  
  334. foreach ($lines as $line_num => $line)
  335. {
  336.  
  337. $pos = strpos(htmlspecialchars($line), $findme);
  338. if ($pos === false)
  339. {
  340. //echo "The string '$findme' was not found in the string '$mystring'";
  341. }
  342. else
  343. {
  344. $data[] = "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
  345. }
  346.  
  347.  
  348.  
  349. }
  350. break;
  351. }
  352. }
  353.  
  354. if(isset($data))
  355. {
  356. $count ++;
  357. echo '<h3 id="bobcontent'.$count.'-title" class="handcursor">'.$thefile.'</h3>';
  358. echo '<div id="bobcontent'.$count.'" class="switchgroup1">';
  359. foreach($data as $linedata)
  360. {
  361. echo $linedata;
  362. }
  363. }
  364.  
  365. echo '</div>';
  366. unset($data);
  367. }
  368.  
  369. }
  370. echo '<div align="right"><A HREF="codesearch.php">Search Again?</A></div>';
  371. }
  372. else
  373. {
  374. echo '
  375. <form action="" method="post">
  376. <p>ILStv.com Code Search<br />
  377. <textarea name="searchvar" cols="100" rows="10"></textarea>
  378. </p>
  379. <p>
  380. <input type="submit" name="Submit" value="Submit" />
  381. </p>
  382. </form>';
  383. }
  384. ?>
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392. <script type="text/javascript">
  393. // MAIN FUNCTION: new switchcontent("class name", "[optional_element_type_to_scan_for]") REQUIRED
  394. // Call Instance.init() at the very end. REQUIRED
  395.  
  396. var bobexample=new switchcontent("switchgroup1", "div") //Limit scanning of switch contents to just "div" elements
  397. bobexample.setStatus('<img src="http://img242.imageshack.us/img242/5553/opencq8.png" /> ', '<img src="http://img167.imageshack.us/img167/7718/closedy2.png" /> ')
  398. bobexample.setColor('darkred', 'black')
  399. bobexample.setPersist(true)
  400. bobexample.collapsePrevious(true) //Only one content open at any given time
  401. bobexample.init()
  402. </script>
  403. </body>

URL: http://www.phpbuilder.com/snippet/detail.php?type=snippet&id=1465

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.