I just got owned by losing my global varible scope when working with include files in php. Had to take a chunk of code out of includes, and put it into a higher level file to get the scope back. I need to learn a work around for this because i hate dealing with messy code.
Tagged: php RSS
-
Nathan Haskins
-
Nathan Haskins
D.R.Y. out those useful PHP scripts
D.R.Y. – ‘Don’t repeat yourself’
I have a few PHP scripts that come in handy on a lot of different sites. The issue I ran into was that if i needed to make an update to one of these useful scripts, I’d have to go find all instances of that script, and update them across many domains.
There’s ways of doing this with ninja SED/grep styles. But a better way may to be make a folder just below the domains directory, and include files from that directory as needed.
In the example below im stepping backwards from the public html directory into my scripts directory.
include('../../../phpIncludes/test.php');Thats about the extent of it, and im sure this can be cleaned up a bit. I was using the scandir() function to find my way to the proper path by echoing out the files of the current directory.
I was also reading open_dir() can cause issues with this technique, and if open_dir() is active it can php scripts from editing lower than the domains’s root. open_dir() is a good safety net, but is also prevents some tricks and techniques that just need to happen. A good example is in WordPress sites (as of 2.9) the Media library will throw errors with open_dir() enabled.
-
Nathan Haskins
old jQuery API scraper script
used this on the jQuery iPhone app to get the data.
(.*?) @s', $page, $cmdBlock); //grabs command block //generates a filtered page content for ($i=0; $i<=sizeof($cmdBlock[0]); $i++) { $content .= $cmdBlock[2][$i]; }//end for //plist friendly filters $content = preg_replace('@ @',' ',$content); $content = preg_replace('@&lt\;@','&lt;', $content); // converts less than to a browser viewable text version $content = preg_replace('@&gt\;@','&gt;', $content); // converts greater than to a browser viewable text version $content = preg_replace('@"@','"',$content); // converts chars to quote $content = preg_replace('@@','',$content); preg_match_all('@(.*?)@s', $content, $cmdNames); //filters the command names preg_match_all('@ <div class="desc">(.*?)</div> @s', $content, $cmdDesc); //filters the command names for ($i=0; $i<=sizeof($cmdBlock[0])-1; $i++) { $final .= ""; $final .= $cmdNames[1][$i]; //need to strip tags $final .= "\n"; $final .= ""; $final .= "<h1>".$cmdNames[1][$i]."</h1>"."<br>"; $final .= "<p>".$cmdDesc[1][$i]."</p>"."<br/>\n"; if (preg_match('@<code class="javascript">(.*?)@s', $cmdBlock[0][$i], $cmdEx)) { $cmdEx = preg_replace('@>\;@','>', $cmdEx); //makes the > text elements $cmdEx = preg_replace('@<\;@','<', $cmdEx); //makes the < text elements //syntax highlighting start $cmdEx = preg_replace('@\$@', '<span class="shDollar">\$</span>', $cmdEx); $cmdEx = preg_replace('@\(@', '<span class="shQuote">(</span>' , $cmdEx); $cmdEx = preg_replace('@\)@', '<span class="shQuote">)</span>' , $cmdEx); $cmdEx = preg_replace('@}@' , '<span class="shCurl">}</span>' , $cmdEx); $cmdEx = preg_replace('@{@' , '<span class="shCurl">{</span>' , $cmdEx); //syntax highlighting end $final .= "<div id="codeBlock">".$cmdEx[1]."</div>"."\n"; } else {$final .= "\n"; }//end if $final .= "\n"; }// end for print <<<_HTML_ \n _HTML_; echo $final; print <<<_HTML_ _HTML_; ?>Well that broke the code block… lol Im not too surprised. Note to self, get file attachments setup for easy archiving.
Zip file here: regex-jquery-api.zip